Skip to content

Instantly share code, notes, and snippets.

@subdigital
Created October 4, 2011 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subdigital/1261793 to your computer and use it in GitHub Desktop.
Save subdigital/1261793 to your computer and use it in GitHub Desktop.
Flipping UIButton
/* 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];
};
@interface UIButton (CHFlipButton)
+ (UIView *)flipButtonWithFirstImage:(UIImage *)firstImage
secondImage:(UIImage *)secondImage
firstTransition:(UIViewAnimationTransition)firstTransition
secondTransition:(UIViewAnimationTransition)secondTransition
animationCurve:(UIViewAnimationCurve)curve
duration:(NSTimeInterval)duration
target:(id)target
selector:(SEL)selector;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment