Search code examples
iosiphoneuibuttonuiviewanimationtransition

Flipping a UIButton On Touch


I am writing an app in xcode and I have a button that when pressed changes colors. I want to make the button flip over when pressed and change its color, for example from red to green. How would I go about doing this?


Solution

  • The easiest way is to use a transition animation.

    [UIView beginAnimations:nil context:nil];
    // self.b is a UIButton; _red is a BOOL ivar
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:self.b cache:YES];
    [self.b setBackgroundColor:(_red ? [UIColor greenColor] : [UIColor redColor])];
    _red = !_red;
    [UIView commitAnimations];
    

    You'll have to fix up that code to change whatever it is about the button that you really want to change; it might not be the background color.