Swift - 当标题有变量时,UIButton动画不起作用

时间:2015-03-19 17:13:08

标签: xcode swift uibutton

我试图为UIButton制作动画。 UIButton是隐藏的,我想滑动它以显示它。

与此同时,我用计数器更改了标题"号码:(自我号码)"

标题发生变化,但动画无效。如果我尝试将标题更改为" title"这样的普通字符串,则可以...

我的代码:

var number = 0

func moveBtn(){
    self.number = self.number + 1
    var yPos = 100

    let myCaption: String = "Number: (\(self.number))"
    self.button.setTitle(myCaption, forState: UIControlState.Normal)

    UIView.animateWithDuration(0.4) {
        self.button.frame.origin.y = yPos
    }
}

1 个答案:

答案 0 :(得分:2)

如果您已在界面构建器中创建了约束,则可以通过IBOutlet将其连接到代码并尝试此操作:

var number = 0
@IBOutlet weak var BottomConstraint: NSLayoutConstraint!

    func moveBtn(){
        self.number = self.number + 1
        var yPos = 100

        let myCaption: String = "Number: (\(self.number))"

        UIView.animateWithDuration(0.4, animations: {
            self.button.frame.origin.y = yPos
        }, completion: { finished in
            self.BottomConstraint.constant = 0
            self.button.setTitle(myCaption, forState: UIControlState.Normal)
        })
    }