0

I have an application with multiple views that transitions to the main screen depending on which button was pressed. My current problem is that if the view is in the middle of animating then when the user selects another button then the whole layout becomes messed up. (ex: the views don't align with the screen meaning that they become a few pixels off)

What I would like to know is if there is a way to check if the view is currently animating and if so just have it animate to the last frame and skip anything in between. Below is a small piece of code that I have just tested based on what I have read on other user asked questions on SO:

-(IBAction)buttonPress:(id)sender
{
    if([selectedView.layer.animationKeys count] > 0)
    {
        [selectedView.layer removeAllAnimations];
    }

// Perform other calculations once the animation has stopped
}
4

1 回答 1

0

有很多方法可以做到这一点,但是......

如果您使用块动画,您可以在动画开始时设置一个“isAnimating”标志并在完成块中再次设置它。您可以从任何地方检查布尔值并根据需要处理案例。

至于需要在动画发生后执行代码,但是

// animation code in some method...
[UIView animateWithDuration:1.0
    delay: 0.0
    options: UIViewAnimationOptionCurveEaseIn
    animations:^{
         isAnimating = YES;
         fooView.alpha = 0.0;
    }
    completion:^(BOOL finished){
         isAnimating = NO;
         [[NSNotificationCenter defaultCenter] postNotificationName:@"FooBeDone" object:nil userInfo:nil]
    }];

-(IBAction)buttonPress:(id)sender {
    if (isAnimating) {
         [[NSNotificationCenter defaultCenter] addObserver:self 
                           selector:@selector(doBar:) 
                               name:@"FooBeDone"
                             object:nil];
         // possibly disable button to prevent multiple taps?
    } else {
         [self doBar];
    }
}

- (void)doBar {
     // do what needs to be done when when the animation is over
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"FooBeDone" object:nil];
     // possibly enable button again
}

编辑:我添加了更多代码来显示可能的通知方法。在您的 IBAction 中创建扩展循环将锁定用户界面,直到循环完成并且您可以返回主运行循环,因此强烈建议避免它。通知应该给您相同的效果,但允许您的主运行循环不受阻碍地继续。

于 2012-04-24T19:50:23.417 回答