I want to create a marquee label in Swift. I tried some codes but could not get it to work.
I also can do this with animation but I have problem with repeating it.>
check this code
var check = true
var speed = 2
var stopWidth = 200.0
override func viewDidLoad() {
label.center.x = view.center.x // Place it in the center x of the view.
label.center.x -= view.bounds.width // Place it on the left of the view with the width = the
Timer.scheduledTimer(timeInterval: TimeInterval(speed),
target: self,
selector: #selector(selectCityViewController.sliderAnimationTime),
userInfo: nil,
repeats: true)
}
@objc func sliderAnimationTime() {
// do what should happen when timer triggers an event
UIView.animate(withDuration: TimeInterval(speed), delay: 0, options: [.curveEaseOut], animations: {
if self.check {
self.check = false
self.label.center.x -= self.view.bounds.width - CGFloat(self.stopWidth)
self.view.layoutIfNeeded()
}else{
self.check = true
self.label.center.x += self.view.bounds.width - CGFloat(self.stopWidth)
self.view.layoutIfNeeded()
}
}, completion: nil)
}