iOS模拟中奖名单循环滚动效果

时间:2022-11-01 17:39:24

本文实例为大家分享了ios模拟中奖名单循环滚动效果的具体代码,供大家参考,具体内容如下

1.动态效果图:

iOS模拟中奖名单循环滚动效果

2.思路:

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import uikit
 
class 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()
 
 }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yuhuashimyy/article/details/78286125