我希望我的collectionView从第一个开始,3秒后自动滚动到第二个,3秒后到第三个,依此类推......我怎么能这样做?下面的代码只能运行一次。它从第一个滚动到第二个,但随后停止。
func scrollToNextCell(){
let cellSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
let contentOffset = myCollectionView.contentOffset;
myCollectionView.scrollRectToVisible(CGRect(x: contentOffset.x + cellSize.width, y: contentOffset.y, width: cellSize.width, height: cellSize.height), animated: true)
}
func startTimer() {
_ = Timer.scheduledTimer(timeInterval: 3.0,
target: self,
selector: #selector(scrollToNextCell),
userInfo: nil,
repeats: false)
}
答案 0 :(得分:-1)
我提前为任何拼写错误道歉(我现在无法访问我的Xcode)。但是这样的事情可能有用:
let timerIntervalForResendingCode = TimeInterval(60)
Timer.scheduledTimer(timeInterval: f3.0,
target: self,
selector: #selector(timerEndedUp),
userInfo: nil,
repeats: false)
}
@objc func timerEndedUp() {
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Right, animated: false)
}
答案 1 :(得分:-1)
在viewDidLoad
:
var timer = NSTimer()
var currentItem: Int = 0
override func viewDidLoad() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(3, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
func updateCounter() {
if currentItem < self.collectionView.numberOfItemInSection(0) {
self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: currentItem, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true)
self.currentItem += 1
} else {
self.timer.invalidate()
}
}
我没有尝试过此代码,但这只是一个例子