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

你可能感兴趣的:(collectionView用定时器)