Mason Ballowe
Mason Ballowe

Reputation: 1866

SWIFT: Animation changes speeds, which I don't want

I'm trying to create a Star Wars style scrolling text for a viewController.

I have the following code:

override func viewDidAppear(_ animated: Bool) {

    UIView.animate(withDuration: 60.0, animations: {
        let width = self.scrollingTextLabel.frame.width
        let height = self.scrollingTextLabel.frame.height
        self.scrollingTextLabel.frame = CGRect(x: 5, y: (180 - height), width: width, height: height)
    }, completion:{ _ in
        self.buttonTextLabel.text = "Play"
    })

}

It works perfectly except for 1 thing, it speeds up, then slows down. This means, it's hard to read at the mid point. Is there a way to make the speed constant?

Upvotes: 0

Views: 543

Answers (1)

TylerP
TylerP

Reputation: 9819

Pass in .curveLinear in the animation options to make the animation have a constant velocity:

UIView.animate(withDuration: 60.0, delay: 0.0, options: .curveLinear, animations: {
    let width = self.scrollingTextLabel.frame.width
    let height = self.scrollingTextLabel.frame.height
    self.scrollingTextLabel.frame = CGRect(x: 5, y: (180 - height), width: width, height: height)
}, completion:{ _ in
    self.buttonTextLabel.text = "Play"
})

Upvotes: 2

Related Questions