iOS优雅地实现上下跑马灯效果

2,147 阅读2分钟

设计思路:

  1. 创建两个UIButton,主要是利用 button, 有backgroundImageimagetitleLabel 属性,可以方便的设置文字和图片,如果想用UILabel,直接替换即可;
  2. 使用Timer计时器,切换数据用;
  3. 设置默认值,如果集合中没有数据,给一个默认的,并且不让触发计时器;
  4. 更新两个Buttonframe,来回切换;
  5. titleLabel 的展示,给当前currentButton设置新值,给另一个firstButton设置currentButton. titleLabel .text的值

然后直接上代码,如果疑问,请留言。

struct BLConstStruct {
    var currentRect: CGRect = CGRect.zero
    var lastRect: CGRect = CGRect.zero
    var count = 1
}

class BLScrollLabelItem: NSObject {
    
    var font: UIFont = BLFont(14)
    var color: UIColor = BLDarkColor
    var imageName: String = ""
}


class BLScrollLabel: UIView {
    
    var dataArray: Array<String>? {
        
        didSet {
            dealLogicMetod()
        }
    }
    
    private lazy var changeTimer: Timer! = {
        
        let timer = Timer.init(timeInterval: 3.0, target: self, selector: #selector(timeCounterMethod), userInfo: nil, repeats: true)
        
        RunLoop.current.add(timer, forMode: RunLoop.Mode.common)
        return timer;
    }()
    
    
    private var constSize: BLConstStruct!
    private var item: BLScrollLabelItem?
    
    
    private lazy var firstButton: UIButton = {
        let button = BLButton(title: "", font: self.item?.font, image: self.item?.imageName, selectedImg: nil, toView: self)
        
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
        button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        button.addTarget(self, action: #selector(scrollLabelEvent(sender:)), for: .touchUpInside)
        
        return button
    }()
    
    private lazy var currentButton: UIButton = {
        let button = BLButton(title: "", font: self.item?.font, image: self.item?.imageName, selectedImg: nil, toView: self)
        
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
        button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        button.addTarget(self, action: #selector(scrollLabelEvent(sender:)), for: .touchUpInside)
        
        return button
    }()
    
    init(item: BLScrollLabelItem, size: CGSize) {
       
        let frame = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
        super.init(frame: frame)
        
        let currentRect = CGRect(x: 10, y: 0, width: size.width-20, height: size.height)
       
        let lastRect = CGRect(x: 10, y: size.height, width: size.width-20, height: size.height)

        constSize = BLConstStruct(currentRect: currentRect, lastRect: lastRect, count: 0)

        self.item = item
        self.clipsToBounds = true;
        self.backgroundColor = UIColor.green;
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func dealLogicMetod() {
        
        firstButton.frame = constSize.currentRect
        firstButton.titleLabel?.font = item?.font
        firstButton.setTitleColor(item?.color, for: .normal)
        firstButton.setImage(BLImageNamed(item?.imageName), for: .normal)
        
        
        guard (dataArray!.isEmpty == false) else {
            firstButton.setTitle("暂无资讯", for: .normal)
            firstButton.isUserInteractionEnabled = false
            return
        }
        
        let model = dataArray!.first
        firstButton.setTitle(model, for: .normal)
        
        currentButton.frame = constSize.lastRect;
        currentButton.titleLabel?.font = item?.font
        currentButton.setTitleColor(item?.color, for: .normal)
        currentButton.setImage(BLImageNamed(item?.imageName), for: .normal)
        
        guard changeTimer.isValid else { return }
        changeTimer.fire()
    }
    
    @objc private func timeCounterMethod() {
        
        if constSize.count < (dataArray!.count-1)
        {
            constSize.count += 1
        } else
        {
            constSize.count = 0
        }
        //由当前显示,渐变到 -self.letSize.height,
        firstButton.frame = constSize.currentRect;
        firstButton.alpha = 8.0
        
        //由底部,渐变到当前显示
  
        currentButton.frame = constSize.lastRect;
        currentButton.alpha = 0.3
        
        let model = dataArray![constSize.count]
        currentButton.setTitle(model, for: .normal)
        
        
        UIView.animate(withDuration: 1.0, animations: {
            
            var frame = self.constSize.currentRect
            frame.origin.y = -frame.height
            self.firstButton.frame = frame;
            self.firstButton.alpha = 0.3
            
            self.currentButton.frame = self.constSize.currentRect;
            self.currentButton.alpha = 0.6
            
        }) { (make) in
            
            self.currentButton.alpha = 1.0
            self.firstButton.alpha = 0.0
            self.firstButton.setTitle(self.currentButton.titleLabel?.text, for: .normal)
        }
    }
    
    @objc private func scrollLabelEvent(sender: UIButton) {
        
    }
    
    deinit {
        if changeTimer.isValid {
            changeTimer.invalidate()
            changeTimer = nil
        }
    }
}

使用

import UIKit

class BLViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let item: BLScrollLabelItem = BLScrollLabelItem()
        item.color = UIColor.darkGray
        item.font = BLFont(17)
        item.imageName = "radio-broadcast"
        
        let frame = CGRect(x: 0, y: 100, width: kScreen_width, height: 50)
        
        let scrollLabel: BLScrollLabel = BLScrollLabel.init(item: item, size: frame.size)
        scrollLabel.frame.origin = frame.origin
        scrollLabel.dataArray = ["首页接口   热门课程——老接口,近期直", "业讲师—讲师列表接口,资讯——资讯接口", "热门课程—"]
        self.view.addSubview(scrollLabel)
    }
}

相关博文:www.jianshu.com/p/5acd184e1…