美文网首页
关于iOS swift3.0 UICollectionView封

关于iOS swift3.0 UICollectionView封

作者: cc帅气的昵称 | 来源:发表于2017-04-09 18:40 被阅读0次

关于swift封装的轮播图和引导页请看链接blog.csdn.net/cheniOSjourney/article/details/69864520

在链接中已经很清楚,这里说下遇到的一些坑,以备参考:

1.重中之重,因为在demo中我用的是GCD定时器所以在定义timer变量的时候,

/// 循环(轮播图模式)

if pageYesAndNo == true {

// 定义需要计时的时间

var timeCount = 0

// 在global线程里创建一个时间源

timer = DispatchSource.makeTimerSource(queue:DispatchQueue.global())

// 设定这个时间源是每秒循环一次,立即开始

timer?.scheduleRepeating(deadline: .now() , interval: .seconds(pageSeconds))

// 设定时间源的触发事件

timer?.setEventHandler {

// 每秒计时一次

timeCount = timeCount + 1

// 返回主线程处理一些事件,更新UI等等

DispatchQueue.main.async {

let offset = CGPoint(x: CGFloat(timeCount % self.imgNameArray.count) * (self.collectionView?.bounds.width)!, y: 0)

self.collectionView?.setContentOffset(offset, animated: true)

self.pageControl.currentPage = timeCount % self.imgNameArray.count

debugPrint("2222")

}

}

// 启动时间源

timer?.resume()

在timer触发事件中,timeCount若为弱引用,它将不继续走子线程,所以要使用强引用的变量

相关文章

网友评论

      本文标题:关于iOS swift3.0 UICollectionView封

      本文链接:https://www.haomeiwen.com/subject/vvadattx.html