How to use MarqueeLabel in Swift?

后端 未结 7 1613
无人及你
无人及你 2021-01-05 19:17

I want to know if there\'s a way to enable horizontal scrolling of text i.e., marquee type text. I have used this library: https://github.com/cbpowell/MarqueeLabel-Swift and

7条回答
  •  情歌与酒
    2021-01-05 19:53

    You should use like this then it will work.

    NOTE: You can face performance issue by this because it's using recursion in Thread, so for better performance you should use with CATextLayer & CAScrollLayer.

    import UIKit
    import SnapKit
    
    class ViewController: UIViewController {
        
        let label = UILabel()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            label.lineBreakMode = .byWordWrapping
            label.numberOfLines = 0
           
            label.text = "We couldn't turn around 'Til we were upside down I'll be the bad guy now But no, I ain't too proud I couldn't be there Even when I try You don't believe it We do this every time"
           
            view.addSubview(label)
            label.snp.makeConstraints { (make) in
               make.height.equalTo(20)
                make.top.leading.trailing.equalTo(self.view.safeAreaLayoutGuide).inset(50)
            }
          
            autoScroll()
        }
        
        @objc func autoScroll() {
            UIView.animate(withDuration: 12.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
                self.label.center = CGPoint(x: 0 - self.label.bounds.size.width / 2,y: self.label.center.y)
            }, completion:  { _ in
                self.autoScroll()
            })
        }
    }
    

提交回复
热议问题