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

所属分类: 软件编程 / IOS 阅读数: 1309
收藏 0 赞 0 分享

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

1.动态效果图:

 

2.思路:

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

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

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

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

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()

 }


}

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

更多精彩内容其他人还在看

IOS 使用Block二次封装AFNetworking 3.0详解

这篇文章主要介绍了IOS 使用Block二次封装AFNetworking 3.0详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS 开发之对象为空的判断(nil、null)详解

这篇文章主要介绍了IOS 开发之对象为空的判断(nil、null)详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS创建对象的不同姿势详解

这篇文章主要介绍了iOS创建对象的不同姿势,文中介绍的很详细,对大家具有一定的参考价值,有需要的朋友们下面来一起学习学习吧。
收藏 0 赞 0 分享

探究iOS多线程究竟不安全在哪里?

iOS多线程安全的概念在很多地方都会遇到,为什么不安全,不安全又该怎么去定义,其实是个值得深究的话题。那么通过下面这篇文章小编和大家一起来探究了iOS多线程究竟不安全在哪里?需要的朋友可以参考学习。
收藏 0 赞 0 分享

IOS购物车界面实现效果示例

本篇文章主要介绍了IOS购物车界面实现效果示例,有需要了解的朋友可参考。希望此文章对各位有所帮助。
收藏 0 赞 0 分享

iOS Touch ID 身份认证

本文主要介绍了iOS Touch ID 身份认证的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS 使用 socket 实现即时通信示例(非第三方库)

这篇文章主要介绍了iOS 使用 socket 即时通信示例(非第三方库)的资料,这里整理了详细的代码,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

ios常见加密解密方法(RSA、DES 、AES、MD5)

本篇文章主要介绍了ios常见加密解密方法(RSA、DES 、AES、MD5),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS利用AFNetworking实现文件上传的示例代码

本篇文章主要介绍了iOS利用AFNetworking实现文件上传的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈IOS中AFNetworking网络请求的get和post步骤

本篇文章主要介绍了浅谈IOS中AFNetworking网络请求的get和post步骤的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享
查看更多