iOS开源一个简单的订餐app UI框架

2020-01-18 16:46:44于丽

先实现一个回调方法,当点击了cell上的购买按钮后触发


func menuListCell(_ cell: MenuListCell, foodImageView: UIImageView)
 {
  guard let indexPath = tableView.indexPath(for: cell) else { return }

  // retrieve the current food model, add it to shopping cart model
  let model = foodArray[indexPath.section][indexPath.row]
  addFoodArray.append(model)
  // recalculate the frame of imageView, start animation
  var rect = tableView.rectForRow(at: indexPath)
  rect.origin.y -= tableView.contentOffset.y
  var headRect = foodImageView.frame
  headRect.origin.y = rect.origin.y + headRect.origin.y - 64
  startAnimation(headRect, foodImageView: foodImageView)
 }

这是点击购买之后的动画实现:


fileprivate func startAnimation(_ rect: CGRect, foodImageView: UIImageView)
 {
  if layer == nil {
   layer = CALayer()
   layer?.contents = foodImageView.layer.contents
   layer?.contentsGravity = kCAGravityResizeAspectFill
   layer?.bounds = rect
   layer?.cornerRadius = layer!.bounds.height * 0.5
   layer?.masksToBounds = true
   layer?.position = CGPoint(x: foodImageView.center.x, y: rect.minY + 96)
   KeyWindow.layer.addSublayer(layer!)

   // animation path
   path = UIBezierPath()
   path!.move(to: layer!.position)
   path!.addQuadCurve(to: CGPoint(x:SCREEN_WIDTH - 25, y: 35), controlPoint: CGPoint(x: SCREEN_WIDTH * 0.5, y: rect.origin.y - 80))
  }
  groupAnimation()
 }

这是放大,缩小,抛入购物车的组动画


 // start group animation: throw, larger, smaller image
 fileprivate func groupAnimation()
 {
  tableView.isUserInteractionEnabled = false

  // move path
  let animation = CAKeyframeAnimation(keyPath: "position")
  animation.path = path!.cgPath
  animation.rotationMode = kCAAnimationRotateAuto

  // larger image
  let bigAnimation = CABasicAnimation(keyPath: "transform.scale")
  bigAnimation.duration = 0.5
  bigAnimation.fromValue = 1
  bigAnimation.toValue = 2
  bigAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)  // smaller image
  let smallAnimation = CABasicAnimation(keyPath: "transform.scale")
  smallAnimation.beginTime = 0.5
  smallAnimation.duration = 1
  smallAnimation.fromValue = 2
  smallAnimation.toValue = 0.5
  smallAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)  // group animation
  let groupAnimation = CAAnimationGroup()
  groupAnimation.animations = [animation, bigAnimation, smallAnimation]
  groupAnimation.duration = 1.5
  groupAnimation.isRemovedOnCompletion = false
  groupAnimation.fillMode = kCAFillModeForwards
  groupAnimation.delegate = self
  layer?.add(groupAnimation, forKey: "groupAnimation")
 }

组动画结束后的一些动画效果。


 // end image animation, start other animations
 func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
 {  if anim == layer?.animation(forKey: "groupAnimation")
  {   // start user interaction
   tableView.isUserInteractionEnabled = true

   // hide layer
   layer?.removeAllAnimations()
   layer?.removeFromSuperlayer()
   layer = nil

   // if user buy any food, show the count label
   if self.addFoodArray.count > 0 {
    addCountLabel.isHidden = false
   }   // show the count label
   let goodCountAnimation = CATransition()
   goodCountAnimation.duration = 0.25
   addCountLabel.text = "(self.addFoodArray.count)"
   addCountLabel.layer.add(goodCountAnimation, forKey: nil)   // shopping cart shaking
   let cartAnimation = CABasicAnimation(keyPath: "transform.translation.y")
   cartAnimation.duration = 0.25
   cartAnimation.fromValue = -5
   cartAnimation.toValue = 5
   cartAnimation.autoreverses = true
   cartButton.layer.add(cartAnimation, forKey: nil)
  }
 }