John Lane
John Lane

Reputation: 1132

iOS Blocks - defining UIView animation-like blocks

I'm trying to create a custom block like the UIView animation blocks. Basically I want to be able to pass either a method or any number of instructions and also provide a completion handler. My question is how would I specify the arguments part of the block definition?

Upvotes: 4

Views: 3212

Answers (2)

nmock
nmock

Reputation: 1917

You can have a method declaration such as:

- (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);
                }
            }];

}

Then, you can call it with:

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

Upvotes: 4

manuelBetancurt
manuelBetancurt

Reputation: 16168

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

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

Upvotes: 2

Related Questions