how Marquee text of label on swift?

后端 未结 4 518
我寻月下人不归
我寻月下人不归 2021-01-17 08:42

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.

4条回答
  •  独厮守ぢ
    2021-01-17 09:06

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

提交回复
热议问题