// MARK: - 商品图片抛入购物车的动画效果
extension QHGoodListViewController {
/**
开始动画
- parameter rect: 商品图标对象的frame
- parameter iconView: 商品图标对象
*/
private func startAnimation(rect: CGRect, iconView:UIImageView) {
if layer == nil {
layer = CALayer()
layer?.contents = iconView.layer.contents
layer?.contentsGravity = kCAGravityResizeAspectFill
layer?.bounds = rect
layer?.cornerRadius = CGRectGetHeight(layer!.bounds)*0.5
layer?.masksToBounds = true
layer?.position = CGPoint(x: iconView.center.x, y: CGRectGetMinY(rect)+96)
UIApplication.sharedApplication().keyWindow?.layer.addSublayer(layer!)
path = UIBezierPath()
path!.moveToPoint(layer!.position)
path?.addQuadCurveToPoint(CGPoint(x: SCREEN_WIDTH - 25, y: 35), controlPoint: CGPoint(x: SCREEN_WIDTH*0.5, y: rect.origin.y - 80))
}
// 组动画
groupAnimation()
}
// 组动画 帧动画抛入购物车 并放大、缩小图层增加点动效
private func groupAnimation() {
// 开始动画禁用tableView交互
tableView.userInteractionEnabled = false
// 帧动画
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = path!.CGPath
animation.rotationMode = kCAAnimationRotateAuto
// 放大动画
let bigAnimation = CABasicAnimation(keyPath: "transform.scale")
bigAnimation.duration = 0.5
bigAnimation.fromValue = 1
bigAnimation.toValue = 2
bigAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
// 缩小动画
let smallAnimation = CABasicAnimation(keyPath: "transform.scale")
smallAnimation.beginTime = 0.5
smallAnimation.duration = 1.5
smallAnimation.fromValue = 2
smallAnimation.toValue = 0.3
smallAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
// 组动画
let groupAnimation = CAAnimationGroup()
groupAnimation.animations = [animation, bigAnimation, smallAnimation]
groupAnimation.duration = 2
groupAnimation.removedOnCompletion = false
groupAnimation.fillMode = kCAFillModeForwards
groupAnimation.delegate = self
layer?.addAnimation(groupAnimation, forKey: "groupAnimation")
}
// 动画结束后做一些操作
override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
// 如果动画是我们的组动画 才开始一些操作
if anim == layer?.animationForKey("groupAnimation") {
// 开启交互
tableView.userInteractionEnabled = true
// 隐藏图层
layer?.removeAllAnimations()
layer?.removeFromSuperlayer()
layer = nil
// 如果商品数大于0 显示购物车里地商品数量
if self.addGoodArray.count > 0 {
addCountLabel.hidden = false
}
// 商品数量渐出
let goodCountAnimation = CATransition()
goodCountAnimation.duration = 0.25
addCountLabel.text = "\(self.addGoodArray.count)"
addCountLabel.layer.addAnimation(goodCountAnimation, forKey: nil)
//购物车颤抖
let cartAnimation = CABasicAnimation(keyPath: "transform.translation.y")
cartAnimation.duration = 0.25
cartAnimation.fromValue = -5
cartAnimation.toValue = 5
cartAnimation.autoreverses = true
cartButton.layer.addAnimation(cartAnimation, forKey: nil)
}
}
}
swift-商品加入购物车动画
最新推荐文章于 2023-11-24 15:28:26 发布