本文实例为大家分享了iOS模拟中奖名单循环滚动效果的具体代码,供大家参考,具体内容如下
1.动态效果图:
https://zsrimg.ikafan.com/file_images/article/201909/201998101109410.gif?201988101119
2.思路:

(1)控件:一个父View,依次添加两个tableVew,使其上下紧挨着,高度均等于所有cell的总高度,且加载相同的的数据,父视图的clipsToBounds属性一定要设置为true

(2)滚动:使用计时器,调整时间及滚动大小,使展示平滑

(3)循环算法:当A列表滚动出界面时,就把它添加在B列表的下面,B列表滚动出界面时,就把它添加在A列表的下面,形成循环效果

3.Swift版核心代码(可直接复制粘贴看效果):
import UIKit

  1. class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
  2. var tableView:UITableView!
  3. var doubleTableView:UITableView!
  4. let kScreenW = UIScreen.main.bounds.size.width
  5. let kXPercent = UIScreen.main.bounds.size.width / 375.0
  6. let kBorderW = CGFloat(15.0)
  7. let kYPercent = UIScreen.main.bounds.size.width / 375.0
  8. let cellId:String = "drawViewCell1"
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. self.addListTableView()
  12. }
  13. func addListTableView(){
  14. let tableWidth = kScreenW - kBorderW*3
  15. let tableBgView = UIView(frame: CGRect(x: (kScreenW-tableWidth)/2.0,y: 100*kYPercent,width: tableWidth,height: 148*kYPercent))
  16. tableBgView.clipsToBounds = true
  17. tableBgView.backgroundColor = UIColor.yellow
  18. self.view.addSubview(tableBgView)
  19. //
  20. tableView = UITableView(frame: CGRect(x: 0,y: 0,width: tableWidth,height: 148*kYPercent*2), style: UITableViewStyle.plain)
  21. tableView.backgroundColor = UIColor.clear
  22. tableView.delegate = self
  23. tableView.dataSource = self
  24. tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  25. tableBgView.addSubview(tableView)
  26. doubleTableView = UITableView(frame: CGRect(x: 0,y: tableView.frame.origin.y+tableView.frame.size.height,width: tableWidth,height: 148*kYPercent*2), style: UITableViewStyle.plain)
  27. doubleTableView.backgroundColor = UIColor.clear
  28. doubleTableView.delegate = self
  29. doubleTableView.dataSource = self
  30. doubleTableView.separatorStyle = UITableViewCellSeparatorStyle.none
  31. tableBgView.addSubview(doubleTableView)
  32. //
  33. Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(personListScroll(timer:)), userInfo: nil, repeats: true)
  34. }
  35. @objc func personListScroll(timer:Timer){
  36. // 1>移动tableView的frame
  37. var newTableViewframe = self.tableView.frame
  38. newTableViewframe.origin.y -= 2*kYPercent
  39. if (newTableViewframe.origin.y < -(doubleTableView.frame.size.height)) {
  40. newTableViewframe.origin.y = tableView.frame.size.height
  41. }
  42. self.tableView.frame = newTableViewframe
  43. // 2>移动doubleTableView的frame
  44. var newDoubleViewframe = self.doubleTableView.frame
  45. newDoubleViewframe.origin.y -= 2*kYPercent
  46. if newDoubleViewframe.origin.y < -(tableView.frame.size.he
  47. newDoubleViewframe.origin.y = tableView.frame.size.height
  48. }
  49. self.doubleTableView.frame = newDoubleViewframe
  50. }
  51. //返回行的个数
  52. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
  53. return 10
  54. }
  55. //返回列的个数
  56. func numberOfSections(in tableView: UITableView) -> Int {
  57. return 1;
  58. }
  59. //去除头部空白
  60. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  61. return 0.001
  62. }
  63. //去除尾部空白
  64. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  65. return 0.001
  66. }
  67. //返回一个cell
  68. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
  69. //回收池
  70. var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellId)
  71. if cell == nil{//判断是否为nil
  72. cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
  73. }
  74. cell.backgroundColor = UIColor.clear
  75. cell.selectionStyle = UITableViewCellSelectionStyle.none
  76. if tableView == self.tableView{// 测试是否循环滚动
  77. cell.textLabel?.text = "张先生"
  78. }else {
  79. cell.textLabel?.text = "李小姐"
  80. }
  81. return cell
  82. }
  83. //返回cell的高度
  84. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
  85. return 148/5.0*kYPercent
  86. }
  87. override func didReceiveMemoryWarning() {
  88. super.didReceiveMemoryWarning()

更多相关文章

  1. android 通过滚动条改变图片显示
  2. 轮播网络图片加载适配
  3. 20150618_Andriod_设置TextView垂直滚动
  4. Android(安卓)R- CarAudioService之registerAudioPolicy动态注册
  5. Android中OnScrollListener回调具体剖析
  6. android 弹出日期滑动选择框,日期滚动滑动选择
  7. Android最新支持包Design简介
  8. android ListView 九大重要属性详细分析
  9. Python中while循环嵌套【4个方面详细讲解】

随机推荐

  1. 如果鼠标在图片上,如何在图片上获取文字?
  2. Html--树莓派作为Web服务器
  3. 像那种以.html为后缀名的网站使用的是什
  4. 使用相同的colgroup时,多个HTML表具有不同
  5. GET错误Glyphicon-halflings-regular.ttf
  6. 绝对定位的div层,别再让flash盖住了
  7. 点击后如何使弹出文本消失?
  8. 使用Objective-C将HTML文本转换为纯文本
  9. html响应式布局_媒体查询
  10. HTML实现图片上传前预览