• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >swift > Swift实现无限轮播效果

Swift实现无限轮播效果

作者:豆丶浆油条 字体:[增加 减小] 来源:互联网 时间:2017-05-28

豆丶浆油条 通过本文主要向大家介绍了taylor swift,swift,swift code是什么意思,taylor swift好听的歌,taylor swift演唱会等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播。广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们用Swift实现一下。项目地址

图片切换我们可以选择的基本控件有两个UIScrollView 和 UICollectionView,这次我们选择UICollectionView;既然是轮播,就会用到Timer。所以,我们这次主要应用的知识点为UICollectionView 和 Timer;

import UIKit

class CycleScrollView: UIView, UICollectionViewDelegate,UICollectionViewDataSource {

  var bottomView : UICollectionView?
  var width : CGFloat?
  var height : CGFloat?
  var timer : Timer?
  
  override init(frame: CGRect){
    
    super.init(frame: frame)
    // 1.设置背景色
    self.backgroundColor = UIColor.clear
    // 2.设置宽高
    width = self.frame.size.width
    height = self.frame.size.height
    // 3.添加bottomView
    setupBottomView()
    // 4.添加定时器
    setupTimer()
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  
  func setupBottomView() {
    
    // 5.设置collectionView的布局
    let flowLayout = UICollectionViewFlowLayout();
    flowLayout.itemSize = self.bounds.size
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal;
    bottomView = UICollectionView.init(frame: self.bounds, collectionViewLayout: flowLayout)
    self.addSubview(bottomView!);
    // 6.设置collectionView的尺寸
    bottomView?.contentSize = CGSize(width:width! * CGFloat(4),height:height!)
    // 7.分页
    bottomView?.isPagingEnabled = true
    // 8.去掉滚动条
    bottomView?.showsVerticalScrollIndicator = false
    bottomView?.showsHorizontalScrollIndicator = false
    // 9.设置代理
    bottomView?.delegate = self
    bottomView?.dataSource = self
    // 10.注册cell
    bottomView?.register(UICollectionViewCell().classForCoder, forCellWithReuseIdentifier: "ID");
    if #available(iOS 10.0, *) {
      // 11.预加载
      bottomView?.isPrefetchingEnabled = true
    } else {
      // Fallback on earlier versions
    }
  }
  func setupTimer() {
    // 12.实例化定时器
    timer = Timer.init(timeInterval: 2, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true);
    RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode);

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
    
      self.timer?.fire();
    }
  }
  func timerAction() {
    
    var contentOffsetX = (self.bottomView?.contentOffset.x)! + self.frame.size.width
    
    if contentOffsetX > self.frame.size.width * 3 {
      // 当前视图显示的是第三个的时候,设置bottomView的偏移量为0
      self.bottomView?.contentOffset = CGPoint(x:0,y:0)
      contentOffsetX = self.frame.size.width
    }
     self.bottomView?.setContentOffset(CGPoint(x:contentOffsetX,y:0), animated: true)
  }
  // 重写removeFromSuperview方法,用于删除定时器,否则定时器一直存在,浪费内存
  override func removeFromSuperview() {
    
    timer?.invalidate()
    timer = nil
    super.removeFromSuperview()
  }
  // Mark:UICollectionViewDataSource
  // 设置Itmes
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    return 4;
  }
  // 设置cell
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ID", for: indexPath)
    for view : UIView in cell.contentView.subviews {
      
      view.removeFromSuperview()
    }
    let imageView = UIImageView.init(frame: cell.contentView.bounds)
    if indexPath.row < 3 {
      
      imageView.image = UIImage.init(named: String(indexPath.row))

    } else {
      imageView.image = UIImage.init(named: String(0))
    }
    cell.contentView.addSubview(imageView)
    
    return cell;
  }
  // Mark:UICollectionViewDelegate
  // 点击方法
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    print("您点击了第 \(indexPath.row == 3 ? 0 : indexPath.row) 个");
  }
  
}

</div>

UICollectionView 和 Timer的用法和OC基本相同。Swift和OC的UI部分应该是一致的,因为底层都是OpenGL。我直接说一下区别:

1.Timer:如果重复,OC是等一个间隔再执行的,Swift是立即执行的,所以我用了GCD延时开启定时器。

2.Swift 没有 CGPointZero。

无限轮播的原理就是在最后面多添加一个和第一个相同的itme。当你滑动到最后一个itme时,把UICollectionView的contentOffset置零,继续向右活动。如果不添加,会给用户一种卡顿的感觉。

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Swift 环境搭建
  • Swift 基本语法
  • Swift 数据类型
  • Swift 变量
  • Swift 常量
  • Swift 运算符
  • Swift 条件语句
  • Swift 循环
  • Swift 字符串
  • Swift 字符(Character)

相关文章

  • 2017-05-28Swift 3.0基础学习之扩展
  • 2017-05-22Swift 可选(Optionals)类型
  • 2017-05-28Swift使用Cocoa中的数据类型教程
  • 2017-05-28Swift实现Selection Sort选择排序算法的实例讲解
  • 2017-05-28Swift项目中利用SWRevealViewController实现侧滑菜单
  • 2017-05-28Swift教程之基础数据类型详解
  • 2017-05-28Swift中的条件判断、循环、跳转语句基础学习笔记
  • 2017-05-28Swift操作Quartz 2D进行简单的绘图与坐标变换的教程
  • 2017-05-28深入理解Swift中的变量与常量
  • 2017-05-28Servlet3.0实现文件上传的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 详解Swift中enum枚举类型的用法
    • Swift 自动引用计数(ARC)
    • iOS开发中Swift 指纹验证功能模块实例代码
    • 简单了解Swift语言中的break和continue语句的用法
    • Swift编程中的switch...case语句实例解析
    • 详解Swift编程中下标的用法
    • MyBatis关于二级缓存问题
    • 查看import的类是出自哪个jar包的方法
    • Swift心得笔记之控制流
    • Swift下使用UICollectionView 实现长按拖拽功能

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有