- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- iOS如何模擬實(shí)現中獎名單循環(huán)滾動(dòng)效果
這篇文章主要介紹了iOS如何模擬實(shí)現中獎名單循環(huán)滾動(dòng)效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
思路:
(1)控件:一個(gè)父View,依次添加兩個(gè)tableVew,使其上下緊挨著(zhù),高度均等于所有cell的總高度,且加載相同的的數據,父視圖的clipsToBounds屬性一定要設置為true
(2)滾動(dòng):使用計時(shí)器,調整時(shí)間及滾動(dòng)大小,使展示平滑
(3)循環(huán)算法:當A列表滾動(dòng)出界面時(shí),就把它添加在B列表的下面,B列表滾動(dòng)出界面時(shí),就把它添加在A(yíng)列表的下面,形成循環(huán)效果
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>移動(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è)數 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return 10 } //返回列的個(gè)數 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{// 測試是否循環(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() }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站