IOS simulation winning list circular scrolling effect

Time:2021-9-20

This example shares the specific code of IOS simulation winning list circular scrolling effect for your reference. The specific contents are as follows

1. Dynamic rendering:

 

2. Ideas:

(1) Control: for a parent view, add two tablevews in turn to make them close up and down, the height is equal to the total height of all cells, and load the same data. The clipstobonds property of the parent view must be set to true

(2) Scroll: use the timer to adjust the time and scroll size to make the display smooth

(3) Loop algorithm: when list a scrolls out of the interface, it is added below list B. when List B scrolls out of the interface, it is added below list a to form a loop effect

3. Swift core code(you can directly copy and paste to see the effect):

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 > move the frame of tableview
 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 > move the frame of doubletableview
 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

 }

 //Returns the number of rows
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
 return 10
 }
 //Returns the number of columns
 func numberOfSections(in tableView: UITableView) -> Int {
 return 1;
 }
 //Remove head blank
 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
 return 0.001
 }
 //Remove trailing blanks
 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
 return 0.001
 }
 //Return a cell
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

 //Recovery tank
 var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellId)

 If cell = = nil {// judge whether it is nil

  cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
 }
 cell.backgroundColor = UIColor.clear
 cell.selectionStyle = UITableViewCellSelectionStyle.none

 If tableview = = self. Tableview {// test whether to scroll circularly

  cell.textLabel?. Text = "Mr. Zhang"
 }else {

  cell.textLabel?. Text = "Miss Li"
 }

 return cell
 }
 //Returns the height of the cell
 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{

 return 148/5.0*kYPercent
 }


 override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()

 }


}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.