在 UIView beginAnimations 完成后执行操作
Posted
技术标签:
【中文标题】在 UIView beginAnimations 完成后执行操作【英文标题】:Doing an action after UIView beginAnimations is finish 【发布时间】:2016-08-02 09:00:27 【问题描述】:我想在 UIView 动画完成后做一个动作。
if (flipStateHav1 == 1 && btnFrontHav1.tag == 1)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainerHav1 cache:YES];
[self.buttonContainerHav1 bringSubviewToFront:btnBackHav1];
[UIView commitAnimations];
//It is this code below I want to run after the animation is finish.
buttonContainerHav1.userInteractionEnabled = YES;
flipTurns = 0;
flipStateHav2 = 0;
par = 0;
我尝试过使用
completion:^(BOOL finished)
// your code
];
但不要让它工作,可能我做错了什么。
更新代码 该按钮连接到两个动作。
- (IBAction) buttonPressedFlipHav1
if (flipTurns == 0 || flipTurns == 1)
flipTurns += 1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainerHav1 cache:YES];
[self.buttonContainerHav1 bringSubviewToFront:btnFrontHav1];
buttonContainerHav1.userInteractionEnabled = NO;
flipStateHav1 = 1;
btnFrontHav1.tag = 1;
autoFlipCount += 1;
if (flipStateHav1 == 1 && flipStateHav2 == 1)
par = 1;
parHav = 1;
btnFrontHav1.tag = 0;
btnFrontHav2.tag = 0;
flipTurns = 0;
autoFlipCount = 0;
if (parHav == 1 && parRiddare == 1 && parSkold == 1)
autoFlipTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(autoFlipAll) userInfo:nil repeats:NO];
completion:^(BOOL finished)
NSLog(@"autoFlipCount = %d",autoFlipCount);
NSLog(@"Flipturns = %d",flipTurns);
];
而且也和这个动作有关。
- (IBAction) autoFlip
autoFlipTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(callAfterFiveSecond) userInfo:nil repeats:NO];
5 秒后它跳转到这个 if 语句。 更新
if (flipStateHav1 == 1 && btnFrontHav1.tag == 1)
[UIView animateWithDuration:0.8 animations:^
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainerHav1 cache:YES];
[self.buttonContainerHav1 bringSubviewToFront:btnBackHav1];
completion:^(BOOL finished)
buttonContainerHav1.userInteractionEnabled = YES;
flipTurns = 0;
flipStateHav1 = 0;
par = 0;
];
【问题讨论】:
UIView 的beginAnimations
不鼓励用于比 ios4 更新的任何东西,您应该改用基于块的动画。
【参考方案1】:
此代码可能对您有所帮助
[UIView animateWithDuration:0.4 animations:^
//animation code here`
completion:^(BOOL finished)
//code after animation here
];
【讨论】:
感谢您的回答。我确实尝试了你的代码。但是持续时间不起作用。翻转动画很快。我试图改变 animateWithDuration:2.0.我做错了什么。 @user3266053 你更新的代码仍然使用beginAnimations,commitAnimations,这是错误的。 谢谢@Abizern,我已经更新了代码,我得到了我想要的结果。以上是关于在 UIView beginAnimations 完成后执行操作的主要内容,如果未能解决你的问题,请参考以下文章
UIView beginAnimations 在自定义属性上太快了