iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
本文實(shí)例為大家分享了iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
1.動(dòng)態(tài)效果圖:
2.思路:
(1)控件:一個(gè)父View,依次添加兩個(gè)tableVew,使其上下緊挨著,高度均等于所有cell的總高度,且加載相同的的數(shù)據(jù),父視圖的clipsToBounds屬性一定要設(shè)置為true
(2)滾動(dòng):使用計(jì)時(shí)器,調(diào)整時(shí)間及滾動(dòng)大小,使展示平滑
(3)循環(huán)算法:當(dāng)A列表滾動(dòng)出界面時(shí),就把它添加在B列表的下面,B列表滾動(dòng)出界面時(shí),就把它添加在A列表的下面,形成循環(huán)效果
3.Swift版核心代碼(可直接復(fù)制粘貼看效果):
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>移動(dòng)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>移動(dòng)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 } //返回行的個(gè)數(shù) func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return 10 } //返回列的個(gè)數(shù) 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 } //返回一個(gè)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{// 測(cè)試是否循環(huán)滾動(dòng) 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() } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資源
欄 目:IOS
下一篇:沒有了
本文標(biāo)題:iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
本文地址:http://mengdiqiu.com.cn/a1/IOS/11882.html
您可能感興趣的文章
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n))
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11iOS調(diào)試Block引用對(duì)象無(wú)法被釋放的小技巧分享
- 01-11iOS動(dòng)態(tài)更換Icon的全過程記錄
- 01-11iOS實(shí)現(xiàn)文本分頁(yè)的方法示例
- 01-11iOS常見宏理解及使用方法
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資源
- 01-11iOS中如何獲取某個(gè)視圖的截圖詳析
- 01-11iOS13適配的實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11UILabel顯示定時(shí)器文本跳動(dòng)問題的解決
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11詳解MacOs免密登錄CentOs操作步驟
- 01-11iOS動(dòng)態(tài)更換Icon的全過程記錄
- 01-11iOS調(diào)試Block引用對(duì)象無(wú)法被釋放的小技
- 01-11iOS常見宏理解及使用方法
- 01-11iOS實(shí)現(xiàn)文本分頁(yè)的方法示例
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什