Created
October 4, 2011 14:34
-
-
Save subdigital/1261793 to your computer and use it in GitHub Desktop.
Flipping UIButton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Here is that block definition from above */ | |
UIButtonFlipActionBlock flipButtonAction = ^(id sender) { | |
//get the alternate button & container | |
UIButton *otherButton = (UIButton *)objc_getAssociatedObject(sender, &UIButtonFlipAltButtonKey); | |
UIView *container = (UIView *)objc_getAssociatedObject(sender, &UIButtonFlipContainerViewKey); | |
//figure out our transition | |
NSNumber *transitionNumber = (NSNumber *)objc_getAssociatedObject(sender, &UIButtonFlipTransitionKey); | |
UIViewAnimationTransition transition = (UIViewAnimationTransition)[transitionNumber intValue]; | |
[UIView animateWithDuration:duration animations:^ { | |
[UIView setAnimationTransition:transition forView:container cache:YES]; | |
[UIView setAnimationCurve:curve]; | |
//the view has the last retain count on the sender button, so we need to retain it first | |
objc_setAssociatedObject(otherButton, &UIButtonFlipAltButtonKey, sender, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
[sender removeFromSuperview]; | |
[container addSubview:otherButton]; | |
//sender no longer needs to retain the other button, because the view now is... | |
objc_setAssociatedObject(sender, &UIButtonFlipAltButtonKey, otherButton, OBJC_ASSOCIATION_ASSIGN); | |
}]; | |
//call the original button handler | |
[target performSelector:selector withObject:self]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment