我的项目中有这个代码:
- (void) fadeImageView {
[UIView animateWithDuration:1.0f
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{
self.imageView.alpha = 0.0f;
}
completion:^(BOOL finished) {
//make the image view un-tappable.
//if the fade was canceled, set the alpha to 1.0
}];
}
但是,有时我想在imageview变得不可见之前取消此操作。有没有办法在动画中期取消这个动画?
答案 0 :(得分:13)
来自Apple文档: 在iOS 4.0及更高版本中不鼓励使用此方法。相反,您应该使用 animateWithDuration:delay:options:animations:completion:
方法来指定动画和动画选项。
[UIView animateWithDuration:1.f
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.imageView.alpha = 0.0f;
} completion:NULL];
答案 1 :(得分:10)
首先,您必须将UIViewAnimationOptionAllowUserInteraction添加到类似的选项中。
- (void) fadeImageView {
[UIView animateWithDuration:1.0f
delay:0
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.imageView.alpha = 0.0f;
}
completion:^(BOOL finished) {
//make the image view un-tappable.
//if the fade was canceled, set the alpha to 1.0
}];
}
然后制作一个这样的方法......
-(void)stopAnimation {
[self.routeView.layer removeAllAnimations];
}
之后当您想要使用.....
删除上述方法的动画调用时[self performSelectorOnMainThread:@selector(stopAnimation) withObject:nil waitUntilDone:YES];
希望它能帮到你
快乐的编码......... !!!!!!!!!!!! :)
编辑:
感谢user1244109指导我。
对于iOS7,我们还需要添加一个选项UIViewAnimationOptionBeginFromCurrentState
,如:
[UIView animateWithDuration:1.0f
delay:0
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.imageView.alpha = 0.0f;
}
completion:^(BOOL finished) {
//make the image view un-tappable.
//if the fade was canceled, set the alpha to 1.0
}];
答案 2 :(得分:8)
更新:首选来自Borut Tomazin的答案https://stackoverflow.com/a/21527129/194309