本文实例为大家分享了iOS模拟中奖名单循环滚动效果的具体代码,供大家参考,具体内容如下1.动态效果图:
2.思路:
(1)控件:一个父View,依次添加两个tableVew,使其上下紧挨着,高度均等于所有cell的总高度,且加载相同的的数据,父视图的clipsToBounds属性一定要设置为true
(2)滚动:使用计时器,调整时间及滚动大小,使展示平滑
(3)循环算法:当A列表滚动出界面时,就把它添加在B列表的下面,B列表滚动出界面时,就把它添加在A列表的下面,形成循环效果
3.Swift版核心代码(可直接复制粘贴看效果): import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{var tableView:UITableView!var doubleTableView:UITableView!let kScreenW = UIScreen.main.bounds.size.widthlet kXPercent = UIScreen.main.bounds.size.width / 375.0let kBorderW = CGFloat(15.0)let kYPercent = UIScreen.main.bounds.size.width / 375.0let cellId:String = "drawViewCell1"override func viewDidLoad() {super.viewDidLoad()self.addListTableView()}func addListTableView(){let tableWidth = kScreenW - kBorderW*3let tableBgView = UIView(frame: CGRect(x: (kScreenW-tableWidth)/2.0,y: 100*kYPercent,width: tableWidth,height: 148*kYPercent))tableBgView.clipsToBounds = truetableBgView.backgroundColor = UIColor.yellowself.view.addSubview(tableBgView)//tableView = UITableView(frame: CGRect(x: 0,y: 0,width: tableWidth,height: 148*kYPercent*2), style: UITableViewStyle.plain)tableView.backgroundColor = UIColor.cleartableView.delegate = selftableView.dataSource = selftableView.separatorStyle = UITableViewCellSeparatorStyle.nonetableBgView.addSubview(tableView)doubleTableView = UITableView(frame: CGRect(x: 0,y: tableView.frame.origin.y+tableView.frame.size.height,width: tableWidth,height: 148*kYPercent*2), style: UITableViewStyle.plain)doubleTableView.backgroundColor = UIColor.cleardoubleTableView.delegate = selfdoubleTableView.dataSource = selfdoubleTableView.separatorStyle = UITableViewCellSeparatorStyle.nonetableBgView.addSubview(doubleTableView)//Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(personListScroll(timer:)), userInfo: nil, repeats: true)}@objc func personListScroll(timer:Timer){// 1>移动tableView的framevar newTableViewframe = self.tableView.framenewTableViewframe.origin.y -= 2*kYPercentif (newTableViewframe.origin.y < -(doubleTableView.frame.size.height)) {newTableViewframe.origin.y = tableView.frame.size.height}self.tableView.frame = newTableViewframe// 2>移动doubleTableView的framevar newDoubleViewframe = self.doubleTableView.framenewDoubleViewframe.origin.y -= 2*kYPercentif newDoubleViewframe.origin.y < -(tableView.frame.size.henewDoubleViewframe.origin.y = tableView.frame.size.height}self.doubleTableView.frame = newDoubleViewframe}//返回行的个数func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{return 10}//返回列的个数func numberOfSections(in tableView: UITableView) -> Int {return 1;}//去除头部空白func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {return 0.001}//去除尾部空白func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {return 0.001}//返回一个cellfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{//回收池var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellId)if cell == nil{//判断是否为nilcell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)}cell.backgroundColor = UIColor.clearcell.selectionStyle = UITableViewCellSelectionStyle.noneif tableView == self.tableView{// 测试是否循环滚动cell.textLabel?.text = "张先生"}else {cell.textLabel?.text = "李小姐"}return cell}//返回cell的高度func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{return 148/5.0*kYPercent}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()
相关推荐
© 2020 asciim码
人生就是一场修行