ios - 制作链式动画的正确方法

标签 ios objective-c ipad

void (^first_animation)();
void (^second_animation)(BOOL finished);


// First animation

first_animation = ^()
{
    g_pin_info_screen.view.alpha = 1.0;
};


// Second animation

second_animation = ^(BOOL finished)
{
    g_shadow_layer.opacity = 0.0;

    void (^set_opacity_to_1)();

    set_opacity_to_1 = ^()
    {
        g_shadow_layer.opacity = 1.0;
    };

    [UIView animateWithDuration : 2.0
            delay               : 0.0
            options             : UIViewAnimationCurveEaseInOut
            animations          : set_opacity_to_1
            completion          : nil
     ];

};



// Begin the animations

{

    float duration;

    duration = 0.35;

    [UIView animateWithDuration : duration
            delay               : 0.00
            options             : UIViewAnimationCurveEaseInOut
            animations          : first_animation
            completion          : second_animation
    ];

}

第一个动画按预期执行。但是第二个动画完成但没有任何动画。

希望有人可以评论上述方案是否是这样做的正确方法。

最佳答案

__block NSMutableArray* animationBlocks = [NSMutableArray new];
typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{

    if ([animationBlocks count] > 0){
        animationBlock block = (animationBlock)[animationBlocks objectAtIndex:0];
        [animationBlocks removeObjectAtIndex:0];
        return block;
    } else {
        return ^(BOOL finished){
            animationBlocks = nil;
        };
    }
};

[animationBlocks addObject:^(BOOL finished){
    [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        //my first set of animations
    } completion: getNextAnimation()];
}];


[animationBlocks addObject:^(BOOL finished){
    [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
       //second set of animations
    } completion: getNextAnimation()];
}];



[animationBlocks addObject:^(BOOL finished){
    [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        //third set
    } completion: getNextAnimation()];
}];


[animationBlocks addObject:^(BOOL finished){
    [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        //last set of animations
    } completion:getNextAnimation()];
}];

// execute the first block in the queue
getNextAnimation()(YES);   

关于ios - 制作链式动画的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17949511/

相关文章:

iphone - 将iOS SDK从5.1降级到5.0

objective-c - NSLog 和 NSLogv 的区别

ios - iPad 上 UIPopoverController 中 UIActionSheet 的自定义背景

iPhone - 删除呈现的 View Controller

ios - 如何将搜索栏添加到 JSQMessagesViewController

objective-c - NSView subview 中断拖动操作

iphone - 如何从 json-rpc web 服务 : iPad/iPhone/Objective C 获取数据

ios - 在 iPad 中从纵向模式切换到横向模式时如何正确加载图像和按钮

ios - 在 Nitrous.io 上修复 Rails 和服务器

ios - Cocoa:命令/usr/bin/ditto 失败,退出代码为 1