简体   繁体   English

Swift-标题包含变量时,UIButton动画不起作用

[英]Swift - UIButton animation doesn't work when title has a variable

I'm trying to animate an UIButton. 我正在尝试为UIButton设置动画。 The UIButton is hidden and I want to slideUp it to show it. UIButton已隐藏,我想滑动它来显示它。

At the same time, i'm changing its title with a counter "Number: (self.number)" 同时,我使用计数器“ Number:(self.number)”更改其标题。

The title changes but the animation doesn't work. 标题更改,但动画不起作用。 If I try to change the title to a normal string like "title", it works... 如果我尝试将标题更改为“ title”之类的普通字符串,则可以使用...

My code: 我的代码:

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
    }
}

if you've created the constraint in the interface builder you connect it to the code through an IBOutlet and try this: 如果您已经在接口构建器中创建了约束,则可以通过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)
        })
    }

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

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