How to cancel UIView block-based animation?

前端 未结 4 1511
栀梦
栀梦 2020-11-29 04:22

I\'ve searched loads of SO stuff and in Apple\'s references, but still unable to manage my problem.

What I have:

  1. A screen with 2 UIImageView
相关标签:
4条回答
  • 2020-11-29 04:49

    I'd add to Nick's answer that to make removeAllAnimations smooth next idea be very handy.

    [view.layer removeAllAnimations];
    [UIView transitionWithView:self.redView
                      duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                          [view.layer displayIfNeeded];
                      } completion:nil];
    
    0 讨论(0)
  • 2020-11-29 04:51

    You can stop all animations on a view by calling:

    [view.layer removeAllAnimations];
    

    (You'll need to import the QuartzCore framework to call methods on view.layer).

    If you want to stop a specific animation, not all animations, your best best bet is to use CAAnimations explicitly rather than the UIView animation helper methods, then you will have more granular control and can stop animations explicitly by name.

    The Apple Core Animation documentation can be found here:

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html

    0 讨论(0)
  • 2020-11-29 04:58

    You can try this (in Swift):

    UIView.setAnimationsEnabled(false)
    UIView.setAnimationsEnabled(true)
    

    Note: you can put code between those two calls if necessary, for example:

    UIView.setAnimationsEnabled(false)
    aview.layer.removeAllAnimations() // remove layer based animations e.g. aview.layer.opacity
    UIView.setAnimationsEnabled(true)
    
    0 讨论(0)
  • 2020-11-29 04:59

    For iOS 10 use UIViewPropertyAnimator to animate. It provides methods to start, stop and pause UIView animations.

     let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeOut){
            self.view.alpha = 0.0
     }
     // Call this to start animation.
     animator.startAnimation()
    
     // Call this to stop animation.
     animator.stopAnimation(true)
    
    0 讨论(0)
提交回复
热议问题