水平旋转(翻转)按钮

时间:2015-06-09 00:33:10

标签: ios swift

我制作了一个测验应用,可让用户选择四个多项选择按钮。我试图让按钮翻转到前面,以便为每个问题显示一组新答案,但我没有取得进展。我发现文档和错误消息极具挑战性。

我包含此代码只是因为我知道我需要包含一些东西;我完全清楚它是垃圾。

func rotateButton(newname:String) {
    let buttonImage = UIImage(named: "yellow_button.png")
    for button in choiceButtons! {
        var new = UIButton()
        new.setImage(UIImage(named: "yellow_button.png"))
        new.setTitle(newname)
        button.addSubview(new)
        UIView.transitionFromView(button, toView: new, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)

2 个答案:

答案 0 :(得分:2)

首先,对于UIButton,没有setImagesetTitle这样的东西,所以你的代码永远不会编译。您必须致电setImage:forState:setTitle:forState:

其次,不要多做一个额外的按钮!没有必要,当你完全忘记按钮时,你只会把自己搞得一团糟。只需更改每个现有按钮的值并翻转它:

UIView.transitionWithView(button, duration: 1,
    options: .TransitionFlipFromLeft | .AllowAnimatedContent,
    animations: {
        button.setImage(UIImage(named: "yellow_button.png"), forState:.Normal)
        button.setTitle(newname, forState:.Normal)
    }, completion: nil)

答案 1 :(得分:0)

感谢马特的回答。

斯威夫特 5:

UIView.transition(with: button, duration: 1,
    options: [.transitionFlipFromLeft, .allowAnimatedContent],
    animations: {
        button.setImage(UIImage(named: "yellow_button.png"), for: .normal)
        button.setTitle(newname, for: .normal)
    }, completion: nil)
相关问题