2

I have a single animation that animates a layer mask successfully:

// 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()

Above works fine. However, right after that, I need to add another CABasicAnimation that gets applied to a different CAShapeLayer.

However, when I add them together as follows, the second animation does not animate, only the first does:

   // 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()

I need both animations to happen simultaneously.

Gizmodo
  • 3,151
  • 7
  • 45
  • 92

1 Answers1

3

I was able to fix this by using 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()
Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • 1
    Good enough for me! (Actually the same idea as what I was trying to do, but never mind why.) – matt Oct 31 '16 at 03:03