[英]cancel a UIView animateWithDuration before completion
我在我的項目中有這個代碼:
- (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變得不可見之前取消此操作。 有沒有辦法取消動畫中期動畫?
來自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];
首先,您必須將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
}];
更新:更喜歡這個答案來自Borut Tomazin的https://stackoverflow.com/a/21527129/194309
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.