Swift 模拟直播间弹幕滚动和渐出效果

  • 先看效果


    我的视频.gif
  • 话不多说上代码
import UIKit

class ViewController: UIViewController {

    var tableView = UITableView()
    
    var timer: Timer?// 定时器
    var indexNum = 0 //滚动下标
    var timeInterval = 2 //时长
    var dataArr = ["我是第1条","我是第2条","我是第3条","我是第4条","我是第5条","我是第6条","我是第7条","我是第8条","我是第9条","我是第10条","我是第11条","我是第12条","我是第13条","我是第14条","我是第15条","我是第16条","我是第17条","我是第18条"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
        tableView.center = self.view.center
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(tableView)
        
        tableView.register(UINib.init(nibName: "textCell", bundle: nil), forCellReuseIdentifier: "textCell")
        
        self.startTimer()
    }

    // 开启滚动定时器
    func startTimer() {
        guard self.timer == nil else {
            return
        }
        self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(timeInterval), target: self, selector: #selector(flipNext), userInfo: nil, repeats: true)
        if let curTime: Timer = timer {
            RunLoop.current.add(curTime, forMode: .common)
        }
    }
    
    // 定时滑动每条消息
    @objc func flipNext() {
        indexNum += 1
        let offset = CGPoint(x:0, y:50 * indexNum)
        self.tableView.setContentOffset(offset, animated: true)
        
        if indexNum >= self.dataArr.count - 1 {
            indexNum = 0
            let offset = CGPoint(x:0, y:50 * indexNum)
            self.tableView.setContentOffset(offset, animated: false)
        }
    }
    
    // 注销定时器
    func cancelTimer() {
        guard self.timer != nil else {
            return
        }
        self.timer!.invalidate()
        self.timer = nil
    }
    
    deinit {
        self.cancelTimer()
    }
}

extension ViewController: UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "textCell", for: indexPath) as! textCell
        cell.label.text = self.dataArr[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    // 滑动开始渐出效果
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.tableView.layer.mask = self.markLayer()
    }
    
    func markLayer() -> CAGradientLayer {
        let gradienLayer = CAGradientLayer.init()
        gradienLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradienLayer.endPoint = CGPoint.init(x: 0, y: 1)
        gradienLayer.colors = [UIColor(white:0xffffff, alpha: 0.05).cgColor, UIColor(white:0xffffff, alpha: 1).cgColor]
        gradienLayer.locations = [0,1.0]
        gradienLayer.rasterizationScale = UIScreen.main.scale
        gradienLayer.frame = self.tableView.bounds
        return gradienLayer
    }
}

码字不易,留个赞吧!~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容