pekpon
pekpon

Reputation: 769

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

I'm trying to animate an UIButton. The UIButton is hidden and I want to slideUp it to show it.

At the same time, i'm changing its title with a counter "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...

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

Upvotes: 0

Views: 750

Answers (1)

jQuero
jQuero

Reputation: 106

if you've created the constraint in the interface builder you connect it to the code through an IBOutlet and try this:

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

Upvotes: 2

Related Questions