Swift Stop Cabasicanimation

Swift stop CABasicAnimation

To remove the animation iterate through sublayers and remove animation for that "pulse" key:

self.layer.sublayers?.forEach {
$0.speed = 0
$0.removeAllAnimations()
$0.removeFromSuperlayer()
}
}

Swift5: Stop CABasicAnimation animation, just when the animation is finished

It looks like you're building the app in the simulator (since I can see the mouse movement), this appears to be a bug that effects simulators only. I was able to reproduce it in the simulator but not on an actual device.

Run it on a device and you should not be seeing that glitch.

Stop CABasicAnimation at specific point

Good question! For this, it's helpful to know the Core Animation architecture.

If you check out the diagram in the Core Animation Programming Guide that describes the Core Animation Rendering Architecture, you can see that there's three trees.

You have the model tree. That's where you set the values of what you want to happen. Then there's the presentation tree. That's what is pretty much happening as far as the runtime is concerned. Then, finally is the render tree. That's what the user sees.

In your case, you want to query the values of the presentation tree.

It's easy to do. For the view that you have attached the animation, get the layer and for that layer, query the presentationLayer's values. For example:

CATransform3D myTransform = [(CALayer*)[self.view.layer presentationLayer] transform];

There's no way to "pause" an animation mid flow. All you can do is query the values, remove it, and then re-create it again from where you left off.

It's a bit of a pain!

Have a look at some of my other posts where I go into this in a bit more detail, e.g.

Restoring animation where it left off when app resumes from background

Don't forget also that when you add an animation to a view's layer, you aren't actually changing the underlying view's properties. So what happens? We'll you get weird effects where the animation stops and you see the view in it's original position.

That's where you need to use the CAAnimation delegates. Have a look at my answer to this post where I cover this:

CABasicAnimation rotate returns to original position

Swift : Pause CABasicAnimation for CALayer

Use this Modified code:

//Global Variables
var layer: CALayer?
var holdGesture = UILongPressGestureRecognizer()
let animation = CABasicAnimation(keyPath: "bounds.size.width")

func setUpView(){

self.view.addGestureRecognizer(holdGesture)
holdGesture.addTarget(self, action:"handleLongPress:")

}

func handleLongPress(sender : UILongPressGestureRecognizer){

if(sender.state == .Began) {

let newLayer = CALayer()
newLayer.frame = CGRect(x: 0, y: 0, width: 0, height: 10)
newLayer.backgroundColor = UIColor.redColor().CGColor

animation.fromValue = 0
animation.toValue = self.view.bounds.width * 2
animation.duration = 5
self.view.layer.addSublayer(newLayer)

print("Long Press Began")
newLayer.addAnimation(animation, forKey: "bounds.size.width")

layer = newLayer
}
else {
print("Long press ended")

if let layer = layer {
pauseLayer(layer)
layer.removeFromSuperlayer()
}
}

}

func pauseLayer(layer : CALayer){
let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime

}

Is there a way to pause a CABasicAnimation?

Recently appeared Apple's technical note QA1673 describes how to pause/resume layer's animation.

Pause and resume animations listing is below:

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}

Edit:
iOS 10 introduced new API - UIViewPropertyAnimator that allows to handle animations more interactively, for example it makes easy to pause and resume animation or 'seek' it to some particular progress value.

Infinite CABasicAnimation stops

Actually, there is a simple way to solve this

setting your animation to this:

Swift 3 version

animation.removedOnCompletion = false

Swift 4 version

animation.isRemovedOnCompletion = false

The layer itself isn't destroyed, when NavigationController is being pushed to another ViewController's view, because UINavigationController got viewControllers property which will retain the original viewController and therefore its view and your animated layer.

It is this CABasicAnimation object destroyed when view removed from interface even though you set its repeatCount to infinite. So set the isRemovedOnCompletion to false to keep it.

How to get solution for stopping/resuming CABasicAnimation working?

Workarounds, workarounds and workarounds. So far, the solution I have got working is to throw away the suggested solution for stoping/resuming layer animation :), to remove all the animations in viewWillDisappear:

[self.myImage.layer removeAllAnimations];  

and then to start a new animation in viewWillAppear. I know that I'm not resuming the old one but, in my case, it's not so visible to the humans eye, so I think I will be good. But if anyone have the solution provided by apple got working, please share.



Related Topics



Leave a reply



Submit