collectionView用定时器

定时器

 NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(didTimer(_:)), userInfo: nil, repeats: true)

didTimer(_:)

    func didTimer(timer: NSTimer) {
        //两种表示方法
        //NSIndexPath
        //位置: frame.origin
        //获取内容的偏移量
        let offset = collectionView.contentOffset
        //根据位置获取Cell
        let indexPath = collectionView.indexPathForItemAtPoint(CGPoint(x: offset.x , y: 0))
        if indexPath!.item == 4 {
            //滚动某个Cell到可见区域
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), atScrollPosition: .Left, animated: false)
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 1,inSection: 0), atScrollPosition: .Left, animated: true)
        }
        else {
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: indexPath!.item + 1, inSection: 0), atScrollPosition: .Left, animated: true)
        }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • @(嵌入式) [TOC] 简述 考虑平台硬件定时器个数限制的, FreeRTOS 通过一个 Daemon 任务(启...
    orientlu阅读 7,994评论 0 3
  • 一、MCS-51单片机的定时器/计数器概念 单片机中的定时器和计数器其实是同一个物理的电子元件,只不过计数器记录的...
    Dorajimmy阅读 1,401评论 0 1
  • 原文链接 你可以使用的定时器类来创建一个定时器对象,或者更简单地说,定时器。一个定时器在指定的时间到达后触发,并发...
    dacheng阅读 1,259评论 0 7
  • 9.26-9.30 第8章 驯服线程和定时器 定时器可以在js中使用,但它不是js的一项功能,如果我们在非浏览器环...
    如201608阅读 602评论 0 2
  • 秋风凉凉,秋夜漫长。庭院之内,桂花飘香。明月当空,看菊花丝长,思人间情暖。享两地情伤。把盏独饮,醉眼迷茫。
    人生入梦_ac07阅读 250评论 0 0