两个不同层次的两个CABasicAnimations

时间:2016-10-31 00:11:25

标签: ios swift calayer cashapelayer cabasicanimation

我有一个动画可以成功激活图层蒙版:

// create new animation
let anim = CABasicAnimation(keyPath: "path")
anim.delegate = self
anim.fromValue = self.maskLayerMWB.path
anim.toValue = path2.CGPath
anim.duration = 0.2
anim.timingFunction = CAMediaTimingFunction(controlPoints: 0, 1, 1, 1)
 self.maskLayerMWB.addAnimation(anim, forKey: nil)

CATransaction.begin()
CATransaction.setDisableActions(true)
 self.maskLayerMWB.path = path2.CGPath
CATransaction.commit()

以上工作正常。但是,在此之后,我需要添加另一个应用于不同CAShapeLayer的CABasicAnimation。

然而,当我按如下方式将它们一起添加时,第二个动画不会动画,只有第一个动画:

   // create new animation
    let anim = CABasicAnimation(keyPath: "path")
    anim.delegate = self
    anim.fromValue = self.maskLayer.path
    anim.toValue = path2.CGPath
    anim.duration = 0.2
     self.maskLayer.addAnimation(anim, forKey: nil)

   //Create second Animation
    let anim2 = CABasicAnimation(keyPath: "path")
    anim2.delegate = self
    anim2.fromValue = self.shapeLayer.path
    anim2.toValue = path2.CGPath
    anim2.duration = 0.2
     self.shapeLayer.addAnimation(anim2, forKey: nil)

    CATransaction.begin()
    CATransaction.setDisableActions(true)
     self.maskLayer.path = path2.CGPath
     self.shapeLayer.path = path2.CGPath
    CATransaction.commit()

我需要两个动画同时发生。

1 个答案:

答案 0 :(得分:3)

我可以使用 CATransaction.setCompletionBlock

解决此问题
    CATransaction.begin()
    CATransaction.setDisableActions(true)

    // create new animation
    let anim = CABasicAnimation(keyPath: "path")
    anim.delegate = self
    anim.fromValue = self.maskLayer.path
    anim.toValue = path2.CGPath
    anim.duration = 0.2
     self.maskLayer.addAnimation(anim, forKey: nil)

   //Create second Animation
    let anim2 = CABasicAnimation(keyPath: "path")
    anim2.delegate = self
    anim2.fromValue = self.shapeLayer.path
    anim2.toValue = path2.CGPath
    anim2.duration = 0.2
     self.shapeLayer.addAnimation(anim2, forKey: nil)

    CATransaction.setCompletionBlock({
       self.maskLayer.path = path2.CGPath
       self.shapeLayer.path = path2.CGPath
    })

    CATransaction.commit()