iOS如何模拟实现中奖名单循环滚动效果

发布时间:2021-09-27 15:21:10 作者:小新
来源:亿速云 阅读:149

这篇文章主要介绍了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如何模拟实现中奖名单循环滚动效果”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. jQuery如何实现中奖播报功能
  2. JS实现的文字间歇循环滚动效果完整示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:如何在谷歌Chrome中禁用代理服务器

下一篇:Java中的反射机制有什么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》