繁体   English   中英

触摸时翻转 UIButton

[英]Flipping a UIButton On Touch

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

ios 12 及以下的 @Matt 解决方案

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM