Wednesday, October 28, 2026
HomeiOS Developmentios - ReplicationLayer stopping animation as soon as the primary occasion finishes...

ios – ReplicationLayer stopping animation as soon as the primary occasion finishes animating, however the different occasion animations are reduce brief

[ad_1]

I’ve connected a gif of what the animation appears to be like like when setup as described beneath. How can I make sure that all 6 cases of the replicatorLayer end animating after which take away the leftover coin picture (i really feel utilizing dispatchQueue.asyncAfter is inadequate for animationPurposes, however am unaware of one other resolution).

Replicated Coins Don't Finish Animating

I’ve a CAReplicationLayer as the primary and solely subLayer of my views major CALayer object.

let replicatorLayer = CAReplicatorLayer()
replicatorLayer.instanceDelay = TimeInterval(0.1)
replicatorLayer.instanceCount = 6
replicatorLayer.body.measurement = body.measurement
layer.addSublayer(replicatorLayer)

I create one other CALayer for a picture and a few textual content and add it as a sublayer replicatorLayer

let coinWithTextLayer = CALayer()
coinWithTextLayer.body = CGRect(x: circleCenter.x, y: circleCenter.y, width: 61, peak: 67)
coinWithTextLayer.masksToBounds = false

let imageLayer = CALayer()
imageLayer.contents = picture.cgImage
imageLayer.body = CGRect(x: 0, y: 20, width: 49, peak: 49)
imageLayer.contentsGravity = .resizeAspect

let textLayer = CATextLayer()
textLayer.body = CGRect(x: 34, y: 0, width: 40, peak: 19)
textLayer.fontSize = 20
textLayer.string = "+ (5)"

coinWithTextLayer.addSublayer(imageLayer)
coinWithTextLayer.addSublayer(textLayer)
replicatorLayer.addSublayer(coinWithTextLayer)

I then apply an animation group to the coinWithTextLayer that animates it up and to the appropriate, whereas additionally scaling from 0.1 to 1

let positionAnim1 = CABasicAnimation(keyPath: "place")
positionAnim1.fromValue = circleCenter
positionAnim1.toValue = CGPoint(x: circleCenter.x + 24, y: circleCenter.y - 24)

let scaleAnim1 = CABasicAnimation(keyPath: "rework.scale")
scaleAnim1.fromValue = 0.1
scaleAnim1.toValue = 1
scaleAnim1.delegate = self

let firstAnimationGroup = CAAnimationGroup()
firstAnimationGroup.beginTime = 0
firstAnimationGroup.period = 2
firstAnimationGroup.autoreverses = false
firstAnimationGroup.animations = [positionAnim1, scaleAnim1]
firstAnimationGroup.delegate = self
firstAnimationGroup.fillMode = .backwards

coinWithTextLayer.add(firstAnimationGroup, forKey: "tapCoinAnim1")
/*DispatchQueue.fundamental.asyncAfter(seconds: 0.5 + (0.1 * 6)) { // 0.1 * 6 to account for the delay between cases of the replicatorLayer
  coinWithTextLayer.removeFromSuperlayer()
  coinWithTextLayer.removeAllAnimations()
}*/ // Commented out as a result of it does successfuly take away the coin after the animation, however seems like an incorrect/inefficient manner of doing so

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments