0

该方法最多接受两个回调,animations并且complete. 如果我定义了类方法中需要做什么,如何将它们传递给animateWithDuration

来自: https ://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/animateWithDuration:animations :

签名看起来像

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

通常它看起来像:

[UIView animateWithDuration:0.2f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     _someView.frame = CGRectMake(-150, -200, 460, 650);
                     _someView.alpha = 0.0;
                 }
                 completion:^(BOOL finished) {
                 }];

所以我正在寻找的是animations在我的课堂上分配一些方法。

选择这种方法作为一个众所周知的例子。然而,根本原因是我有自己的类尝试执行相同的方法,我想知道是否可以通过不在回调中调用类方法来保持代码更简洁。

我不喜欢的最重要的事情是我NSError **用于回调的参数,该参数在发生错误时被调用。子调用将需要某种全局变量,我真的不喜欢那样。所以这是我的完整性签名:

- (void)registerWithEmail:(NSString *)email
                     name:(NSString *)name
             willRegister:(void (^)(void))willRegister
              didRegister:(void (^)(void))didRegister
            registerError:(void (^)(NSError**))registerError;
4

2 回答 2

1

我还认为您的签名是错误的,并且您将对 NSError 对象的间接引用与块混合在一起。它应该是:

- (void) registerWithEmail:(NSString *)email
                 name:(NSString *)name
         willRegister:(void (^)(void))willRegister
          didRegister:(void (^)(void))didRegister
        registerError:(void (^)(NSError*))registerError;

...你会这样称呼它:

[self registerWithEmail:@"email@email.com" name:@"dave" willRegister:^{
    //.. will register
} didRegister:^{
    //.. did register
} registerError:^(NSError *error) {
    // do somethign with the error
}];

如果您想使用对 NSError 变量的间接引用,您的签名可能看起来更像:

- (BOOL)registerWithEmail:(NSString *)email
                 name:(NSString *)name
         willRegister:(void (^)(void))willRegister
          didRegister:(void (^)(void))didRegister
        registerError:(NSError**)registerError;

..你会这样称呼它:

NSError *error = nil;
BOOL result = [self registerWithEmail:@"email@email.com" name:@"dave" willRegister:^{
    //.. will register
} didRegister:^{
    //.. did register
} registerError:&error];

if (!result && error) {
    //Do somethign with the error
}
于 2013-09-03T19:50:17.603 回答
1

只需从块内调用该方法。

    [UIView animateWithDuration:0.4
                 animations:^{
                     [self yourMethod];
                     //...or
                     [YourClass yourMethod]; // as you said 'class' methods 
                 }
                 completion:^(BOOL finished) {
                     [self yourCompletion];
                 }];
于 2013-09-03T19:20:00.327 回答