iOS块 - 定义类似UIView动画的块

时间:2012-11-26 00:44:05

标签: objective-c ios cocoa-touch cocoa objective-c-blocks

我正在尝试创建像UIView动画块一样的自定义块。基本上我希望能够传递方法或任意数量的指令,并提供完成处理程序。我的问题是如何指定块定义的参数部分?

2 个答案:

答案 0 :(得分:4)

您可以拥有方法声明,例如:

- (void) performAnimationWithCompletion:(void (^)(BOOL finished))completion {

[UIView animateWithDuration:0.5 animations:^{
            // your own animation code 
            // ...
            } completion:^(BOOL finished) {
                // your own completion code

                // if completion block defined, call it
                if(completion){
                    completion(YES);
                }
            }];

}

然后,你可以用:

来调用它
[instance performAnimationWithCompletion:^(BOOL complete){
      // define block code to be executed on completion
}];

答案 1 :(得分:2)

[UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         // other animations here

                     }
                     completion:^(BOOL finished){
                         // ... completion stuff
                     }
     ];