iphone - 执行多个连续 UIView 动画的最佳方法?

标签 iphone uiview core-animation

我有一系列 8 个 UIView 动画,这些动画在我的 View 加载后立即发生。现在,我通过使用animationDidStop:finished:context 委托(delegate)方法来完成此任务,一切都按预期进行。问题是我对每个动画都有一个新方法。这些方法中的大部分代码都是重复的,只有动画持续时间和元素的实际位置发生变化。

我尝试创建一个将被调用的方法,并让上下文保存适当更改 UI 所需的参数,但它似乎递归地调用自身,远远超过了我调用它的次数:

-(void)animationDidStop:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
    NSNumber *number = (NSNumber *)context;
    int animationStep = [number intValue];
    int nextAnimationStep = animationStep + 1;
    NSNumber *nextAnimationStepNumber = [NSNumber numberWithInt:nextAnimationStep];
    NSLog(@"Animation Step: %i", animationStep);

    CGRect firstFrame = CGRectMake(self.feedsScroll.frame.size.width * 2, 0.0f, self.secondFeedView.view.frame.size.width, self.secondFeedView.view.frame.size.height);
    CGRect thirdFrame = CGRectMake(self.feedsScroll.frame.size.width * 2, 0.0f, self.thirdFeedView.view.frame.size.width, self.thirdFeedView.view.frame.size.height);

    [UIView beginAnimations:nil context:nextAnimationStepNumber];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];

    if (animationStep < 8) 
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    NSLog(@"Beginning animations");

    switch (animationStep) {
        case 0:
            [UIView setAnimationDuration:.3];
            self.firstFeedView.view.center = CGPointMake(self.firstFeedView.view.center.x + 30, self.firstFeedView.view.center.y);
            break;
        case 1:
            [UIView setAnimationDuration:.3];
            self.firstFeedView.view.center = CGPointMake(self.firstFeedView.view.center.x  - 30, self.firstFeedView.view.center.y);
            break;
        case 2:
            [UIView setAnimationDuration:.3];
            [self.secondFeedView.view setFrame:firstFrame];
            break;
        case 3:
            [UIView setAnimationDuration:.3];
            self.secondFeedView.view.center = CGPointMake(self.secondFeedView.view.center.x + 30, self.firstFeedView.view.center.y);
            break;
        case 4:
            [UIView setAnimationDuration:.3];
            self.secondFeedView.view.center = CGPointMake(self.secondFeedView.view.center.x - 30, self.firstFeedView.view.center.y);
            break;
        case 5:
            NSLog(@"Animation step 6");
            [UIView setAnimationDuration:.5];
            self.firstFeedView.view.center = CGPointMake(self.firstFeedView.view.center.x - 230, self.firstFeedView.view.center.y);
            self.secondFeedView.view.center = CGPointMake(self.secondFeedView.view.center.x - 230, self.firstFeedView.view.center.y);
            break;
        case 6:
            [UIView setAnimationDuration:.5];
            [self.thirdFeedView.view setFrame:thirdFrame];
            break;
        case 7:
            [UIView setAnimationDuration:.3];
            self.thirdFeedView.view.center = CGPointMake(self.thirdFeedView.view.center.x + 30, self.firstFeedView.view.center.y);
            break;
        case 8:
            [UIView setAnimationDuration:.3];
            self.thirdFeedView.view.center = CGPointMake(self.thirdFeedView.view.center.x - 30, self.thirdFeedView.view.center.y);
            break;
        default:
            break;
    }

    [UIView commitAnimations];  
}

我知道这可能是一个幼稚的实现。我是 iPhone 开发新手,正在寻找一些可以在这里应用的最佳实践。我是否以错误的方式处理这个问题?

最佳答案

如果您愿意升级到 iOS 4.0,新的基于 block 的动画方法可以使动画链接变得微不足道。例如:

[UIView animateWithDuration:1.0 animations:^{ view.position = CGPointMake(0.0f, 0.0f); } completion:^(BOOL finished){
    [UIView animateWithDuration:0.2 animations:^{ view.alpha = 0.0; } completion:^(BOOL finished){
        [view removeFromSuperview]; }];
}]

将导致 view 在 1 秒的时间内动画到 (0, 0),淡出 0.2 秒,然后从其 super View 中删除。这些动画将是连续的。

+animateWithDuration:animations:completion: 类方法中的第一个 block 包含立即进行动画处理的属性更改,第二个 block 是动画完成时要执行的操作。由于此回调可以包含此类的另一个动画,因此您可以嵌套它们以创建任意动画链。

关于iphone - 执行多个连续 UIView 动画的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3849460/

相关文章:

iphone - 如何选择 UIWebView 中显示的 PDF 中的文本?

iphone - iPhone中如何实现 View 滑入滑出?

ios - 缩放后的问题我想要完整图像,而不仅仅是缩放图像的一部分

cocoa - 打印CAL层

macos - viewDidLoad 中层支持的 NSView 问题

iphone - iOS: "open: Permission denied"

ios - 使用 SDWebImage 处理超大图像的下载

iphone - 重现时间轴的动画效果,类似于Google plus iOS应用中的动画效果

ios - 当跨任何事件导航到特定 UIViewcontroller 时,应用程序仅在 iOS 11 中崩溃

iphone - 我怎样才能像 UIActionSheet 一样从屏幕底部呈现一个 UIView?