ios 如何取消 UIView 基于块的动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9569943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cancel UIView block-based animation?
提问by raistlin
I've searched loads of SO stuff and in Apple's references, but still unable to manage my problem.
我已经搜索了大量 SO 内容和 Apple 的参考资料,但仍然无法解决我的问题。
What I have:
我拥有的:
- A screen with 2
UIImageView
s and 2UIButton
s connected to them - 2 kinds of animation:
- Scaling up and then down of each image, one after another, only once in
viewDidLoad
- When a button pressed (a custom button hidden 'inside' of each
UIImageView
) it triggers animation of appropriateUIImageView
–only one, not both–(also scale up, then down). - As I am writing for iOS4+ I'm told to use block based animations!
- Scaling up and then down of each image, one after another, only once in
- 一个有 2
UIImageView
s 和 2UIButton
s 连接到它们的屏幕 - 2种动画:
- 放大然后缩小每个图像,一个接一个,只有一次
viewDidLoad
- 当按下一个按钮(每个按钮隐藏在“内部”的自定义按钮
UIImageView
)时,它会触发适当的动画——UIImageView
只有一个,而不是两个——(也按比例放大,然后缩小)。 - 当我为 iOS4+ 写作时,我被告知要使用基于块的动画!
- 放大然后缩小每个图像,一个接一个,只有一次
What I need:
我需要的:
How do I cancel a running animation? I've managed to cancel after all but the last one... :/
如何取消正在运行的动画?除了最后一个之外,我已经设法取消了......:/
Here is my code snippet:
这是我的代码片段:
[UIImageView animateWithDuration:2.0
delay:0.1
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
isAnimating = YES;
self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 2.0, 2.0);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 0.5, 0.5);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 2.0, 2.0);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 0.5, 0.5);
}
completion:^(BOOL finished){
if (!finished) return;
//block letter buttons
[self.bigLetterButton setUserInteractionEnabled:YES];
[self.smallLetterButton setUserInteractionEnabled:YES];
//NSLog(@"vieDidLoad animations finished");
}];
}];
}];
}];
Somehow the smallLetter
UIImageView
is not working properly, because when pressed (through button) bigLetter
is canceling animations properly...
不知何故smallLetter
UIImageView
无法正常工作,因为按下(通过按钮)时bigLetter
正在正确取消动画......
EDIT:I've used this solution, but still having problem with scaling down smallLetter
UIImageView
- not cancelling at all...
solution
编辑:我已经使用了这个解决方案,但仍然有缩小问题的问题smallLetter
UIImageView
- 根本没有取消......
解决方案
EDIT2:I've added this at the beginning of next/prev methods:
EDIT2:我在 next/prev 方法的开头添加了这个:
- (void)stopAnimation:(UIImageView*)source {
[UIView animateWithDuration:0.01
delay:0.0
options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
animations:^ {
source.transform = CGAffineTransformIdentity;
}
completion:NULL
];
}
problem stays... :/ no idea how to interrupt last animation for letters in animation chain
问题仍然存在...:/不知道如何中断动画链中字母的最后一个动画
回答by Nick Lockwood
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).
(您需要导入 QuartzCore 框架来调用 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.
如果你想停止一个特定的动画,而不是所有的动画,最好的办法是明确地使用 CAAnimations 而不是 UIView 动画辅助方法,那么你将有更精细的控制,可以按名称明确地停止动画。
The Apple Core Animation documentation can be found here:
可以在此处找到 Apple Core Animation 文档:
回答by Hari Kunwar
For iOS 10 use UIViewPropertyAnimator to animate. It provides methods to start, stop and pause UIView animations.
对于 iOS 10,使用 UIViewPropertyAnimator 来制作动画。它提供了启动、停止和暂停 UIView 动画的方法。
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)
回答by Gaston
I'd add to Nick's answer that to make removeAllAnimations smooth next idea be very handy.
我要补充尼克的回答,使 removeAllAnimations 平滑下一个想法非常方便。
[view.layer removeAllAnimations];
[UIView transitionWithView:self.redView
duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[view.layer displayIfNeeded];
} completion:nil];
回答by iAmcR
You can try this (in Swift):
你可以试试这个(在 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)