【问题标题】:Flipping a UIButton On Touch触摸时翻转 UIButton
【发布时间】:2014-02-11 00:03:48
【问题描述】:

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

【问题讨论】:

  • google "UIView 块动画" 和 "CGAffineTransform 参考"。

标签: ios iphone uibutton uiviewanimationtransition


【解决方案1】:

适用于 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

【讨论】:

  • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2015-07-26
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多