iOS如何模拟实现中奖名单循环滚动效果
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章主要介绍了iOS如何模拟实现中奖名单循环滚动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。思路:(1)控件:一个父View
千家信息网最后更新 2025年02月01日iOS如何模拟实现中奖名单循环滚动效果
这篇文章主要介绍了iOS如何模拟实现中奖名单循环滚动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
思路:
(1)控件:一个父View,依次添加两个tableVew,使其上下紧挨着,高度均等于所有cell的总高度,且加载相同的的数据,父视图的clipsToBounds属性一定要设置为true
(2)滚动:使用计时器,调整时间及滚动大小,使展示平滑
(3)循环算法:当A列表滚动出界面时,就把它添加在B列表的下面,B列表滚动出界面时,就把它添加在A列表的下面,形成循环效果
3.Swift版核心代码(可直接复制粘贴看效果):
import UIKitclass ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ var tableView:UITableView! var doubleTableView:UITableView! let kScreenW = UIScreen.main.bounds.size.width let kXPercent = UIScreen.main.bounds.size.width / 375.0 let kBorderW = CGFloat(15.0) let kYPercent = UIScreen.main.bounds.size.width / 375.0 let cellId:String = "drawViewCell1" override func viewDidLoad() { super.viewDidLoad() self.addListTableView() } func addListTableView(){ let tableWidth = kScreenW - kBorderW*3 let tableBgView = UIView(frame: CGRect(x: (kScreenW-tableWidth)/2.0,y: 100*kYPercent,width: tableWidth,height: 148*kYPercent)) tableBgView.clipsToBounds = true tableBgView.backgroundColor = UIColor.yellow self.view.addSubview(tableBgView) // tableView = UITableView(frame: CGRect(x: 0,y: 0,width: tableWidth,height: 148*kYPercent*2), style: UITableViewStyle.plain) tableView.backgroundColor = UIColor.clear tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = UITableViewCellSeparatorStyle.none tableBgView.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.clear doubleTableView.delegate = self doubleTableView.dataSource = self doubleTableView.separatorStyle = UITableViewCellSeparatorStyle.none tableBgView.addSubview(doubleTableView) // Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(personListScroll(timer:)), userInfo: nil, repeats: true) } @objc func personListScroll(timer:Timer){ // 1>移动tableView的frame var newTableViewframe = self.tableView.frame newTableViewframe.origin.y -= 2*kYPercent if (newTableViewframe.origin.y < -(doubleTableView.frame.size.height)) { newTableViewframe.origin.y = tableView.frame.size.height } self.tableView.frame = newTableViewframe // 2>移动doubleTableView的frame var newDoubleViewframe = self.doubleTableView.frame newDoubleViewframe.origin.y -= 2*kYPercent if newDoubleViewframe.origin.y < -(tableView.frame.size.height) { newDoubleViewframe.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 } //返回一个cell func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ //回收池 var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellId) if cell == nil{//判断是否为nil cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId) } cell.backgroundColor = UIColor.clear cell.selectionStyle = UITableViewCellSelectionStyle.none if 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() }}
感谢你能够认真阅读完这篇文章,希望小编分享的"iOS如何模拟实现中奖名单循环滚动效果"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
循环
效果
篇文章
高度
名单
空白
移动
相同
平滑
上下
两个
个数
代码
价值
兴趣
同时
大小
头部
小姐
尾部
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
战双帕弥什是什么服务器
数据网络安全公开课
做软件开发是不是英文要好
漏洞数据库
杭州云盾互联网科技有限公司
取指定数据库所有表名
服务器采购建议
数据库技术的迅猛发展
无法创建id服务器故障
广州博奥网络技术有限公司
酒店网络技术维护人员
网络安全观是
复制传递和比较数据库
机动战士高达ol数据库
网络安全中闭合是啥意思
怎么打开网吧电影服务器
数据库中状态为127
软件开发的专业有哪些
携手网络安全给孩子
2021年西安市网络安全
网络技术的股价
电教馆网络安全工作职责
centos数据库文件目录
好贸上海网络技术阿里巴巴
开发机器人有什么软件开发
三万的服务器机柜可以防辐射吗
我的世界服务器怎么安全验证
手机网络安全威胁
南通服务器机柜定做
服务器和互联网安全