Pergunta

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?

Foi útil?

Solução 2

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.

Outras dicas

@Matt Solution for ios 12 and less

UIView.beginAnimations(nil, context: nil)
UIView.setAnimationTransition(UIViewAnimationTransition.FlipFromLeft, forView: self.myButton, cache: true)
self.myButton.backgroundColor = myBOOL ? UIColor.greenColor() : UIColor.redColor()
self.myButton.setTitleColor(myBOOL ? UIColor.redColor() : UIColor.greenColor(), forState: UIControlState.Normal)
myBOOL = !myBOOL
UIView.commitAnimations()

FlipButton_Swift

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top