2017-08-01 103 views
0

我已经写了这个动画的CAShapeLayer(pulseLayer)和viewDidLoad() CAShapeLayer奇怪的行为:在第一时间动画

let pulseLayer = CAShapeLayer() 
@IBOutlet weak var btnCart: UIButton! 

override func viewDidLoad() { 
    let longpress = UILongPressGestureRecognizer(target: self, action: #selector(CategoryViewController.longPressGestureRecognized(_:))) 
    tableView.addGestureRecognizer(longpress) 

    heartBeatAnimation.duration  = 0.75 
    heartBeatAnimation.repeatCount = Float.infinity 
    heartBeatAnimation.autoreverses = true 
    heartBeatAnimation.fromValue  = 1.0 
    heartBeatAnimation.toValue  = 1.2 
    heartBeatAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn) 

    btnCart.layer.addSublayer(pulseLayer) 
} 

func addBtnCartLayerWithAnimation() { 
    let ovalPath = UIBezierPath(arcCenter: CGPoint(x: btnCart.frame.midX, y: btnCart.frame.midY), radius: btnCart.frame.width * 1.5, startAngle: 0*(CGFloat.pi/180), endAngle: 360*(CGFloat.pi/180), clockwise: true) 

    pulseLayer.path = ovalPath.cgPath 
    pulseLayer.opacity = 0.15 
    pulseLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5) 
    pulseLayer.bounds = ovalPath.cgPath.boundingBox 
    pulseLayer.add(heartBeatAnimation, forKey: "heartBeatAnimation") 
    pulseLayer.transform = CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 1.0) 
} 

和removeLayer功能是:

func removeLayer() { 
    pulseLayer.transform = CATransform3DScale(CATransform3DIdentity, 0.1, 0.1, 0.1) 
    pulseLayer.removeAllAnimations() 
} 

问题是当图层的第一个动画来自视图的底部时!

the first animation after viewDidLoad

那之后这个动画与开始从中心(所定义的锚点)任何调用

any animation after the first one

谁能告诉我,为什么会这样呢?

其中I定义且上的tableView用于UILongGestureRecognizer启动/停止动画整个类:

func longPressGestureRecognized(_ gestureRecognizer: UIGestureRecognizer) { 
    let longPress = gestureRecognizer as! UILongPressGestureRecognizer 
    let state = longPress.state 
    let locationInView = longPress.location(in: tableView) 
    let indexPath = tableView.indexPathForRow(at: locationInView) 

    switch state { 
    case UIGestureRecognizerState.began: 
     if indexPath != nil { 
      addBtnCartLayerWithAnimation() 
     } 

    case UIGestureRecognizerState.changed: 
     // some code here not related to the animation 
    default: 
     removeLayer() 
    } 
} 

回答

1

当使用独立的层(层不属于UIView的后备存储器),implicit animations添加用于每一个动画的属性都会改变。

您会看到这种效果,因为该图层将其属性从零动画到您在addBtnCartLayerWithAnimation()中设置的初始值。

你想要做的是设置这些初始值没有动画(这需要明确地完成)。你可以用在交易过程中禁用像这样的动画的变化:

CATransaction.begin() 
CATransaction.setDisableActions(true) 

pulseLayer.path = ovalPath.cgPath 
pulseLayer.opacity = 0.15 
pulseLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5) 
pulseLayer.bounds = ovalPath.cgPath.boundingBox 
pulseLayer.transform = CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 1.0) 

CATransaction.commit() 

pulseLayer.add(heartBeatAnimation, forKey: "heartBeatAnimation") 
+0

后,我加入你的代码,我已经失去了第一缩放动画,然后在第二缩放动画回报,后来! –

+0

什么是'heartBeatAnimation'的代码? – deadbeef

+0

你将不得不显示所有的代码(你在哪里/如何开始/停止动画,...) – deadbeef