如何取消UIViews基于块的animation?
我目前主要关注以下问题:
我开始一个animation,其中2个对象属性被触发。
代码是:
[UIView animateWithDuration:0.3 animations:^{ greyscaleImage.alpha = 1; activityIndicator.alpha = 1; } completion:^(BOOL f){ if(f) { [activityIndicator startAnimating]; } }];
这工作正常。
我发现唯一的问题是,我有一个0.3秒的变化,崩溃的应用程序时,持有这个activityIndicator和greyscaleImage的视图被释放。
为了使它更清晰,请设想一下ViewController,它的视图通过默认的iOS-mode-View方式呈现。 现在触发该animation,需要2分钟。 在达到那2分钟之前,你会发现animation很无聊,你想消除这种观点。 现在,视图,activityIndicator和greyscaleImage被释放,animationo / c不知道该怎么做。
所以我想知道,在这里做什么+为什么debugging指向
} completion:^(BOOL f){
而不是例如[activityIndicator …
有没有办法,让用户在2分钟之前解散视图?
最好的祝福
如果你开始一个新的animation需要0.0秒,并进入你想要去的状态,它将取消旧的animation,并开始新的(即时)“animation”。
例如,如果您想要停止移动视图,请转到已经位于的位置:
[UIView animateWithDuration:0.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{movingView.frame = ((CALayer *)movingView.layer.presentationLayer).frame;} completion:^(BOOL finished){} ];
选项:UIViewAnimationOptionBeginFromCurrentState很重要。 不调用它会让你的animation在前一个animation的结束状态下开始。 在运动中,它会翘曲到最后的位置,然后翘曲到你想要停止的地方。 即使你的取消“animation”是即时的,来回跳动可能是可见的。
注意:animation时间不一定是0.0秒,任何animation都会取消旧animation。 不完全确定不同types的animation。 例如,我不知道改变框架是否会停止淡入淡出。
您可以从视图图层中删除所有animation
[movingView.layer removeAllAnimations];
块在启动后总是会完成,并且不能停止(除非你崩溃了应用程序)。 您可能想要使用通知中心或NSTimer手动更改框架。
设置animation选项UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
为我工作。
像这样使用它:
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{ // Do your stuff here, like changing the frame etc. self.containerView.frame = CGRectMake(0, 100, 300, 300); } completion:nil];