0
votes

The method accepts up to two callbacks, animations and complete. If I define what needs to be done in class methods, how to pass them to animateWithDuration?

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

the signature looks like

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

Usually it looks something like:

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

So what I am looking for is assigning animations to some method in my class.

This method is chosen as an well-known example. However the root is that I have my own class that tries to do the same approach and I was wondering if I could keep code cleaner by not calling class methods within callbacks.

The biggest thing that I don't like is NSError ** parameter I am using for my callback that is called in case of errors. Subcalls will require some sort of global variable and I really don't like that. So here is my signature for completeness:

- (void)registerWithEmail:(NSString *)email
                     name:(NSString *)name
             willRegister:(void (^)(void))willRegister
              didRegister:(void (^)(void))didRegister
            registerError:(void (^)(NSError**))registerError;
2
On a side note, you should have a BOOL return type when you use an error parameter like this. Callers of the method check the return value for success/fail to decide if they should read the registerError variable. See the NSError programming guide... might be a compiler warning now i think...bandejapaisa

2 Answers

1
votes

Just call the method from within the block.

    [UIView animateWithDuration:0.4
                 animations:^{
                     [self yourMethod];
                     //...or
                     [YourClass yourMethod]; // as you said 'class' methods 
                 }
                 completion:^(BOOL finished) {
                     [self yourCompletion];
                 }];
1
votes

I also think your signature is wrong, and you are mixing up indirect references to NSError objects with blocks. It should be:

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

...and you would call it like so:

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

If you wanted to use the indirect reference to the NSError variable, your signature might look something more like:

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

..and you would call this like so:

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

if (!result && error) {
    //Do somethign with the error
}