翻转UIButton On Touch

时间:2014-02-11 00:03:48

标签: ios iphone uibutton uiviewanimationtransition

我正在用xcode编写一个应用程序,我有一个按钮,按下时会改变颜色。我想按下按钮翻转并改变颜色,例如从红色变为绿色。我该怎么做呢?

2 个答案:

答案 0 :(得分:2)

@ Swt的解决方案#

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

答案 1 :(得分:0)

最简单的方法是使用过渡动画。

[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];

您必须修改该代码才能更改您真正想要更改的按钮;它可能不是背景颜色。