我正在尝试在我的滚动视图中自动滚动幻灯片。实际上,我这样做是为了我的入职屏幕。然而,我希望计时器在用户触摸幻灯片时暂停一段时间,并在他移走手指时再次启动。
我正在做这样的事情,但这对我所问的情况不起作用:
in viewDidLoad -
timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)
@objc func autoScroll() {
let totalPossibleOffset = CGFloat(topImages.count - 1) * self.view.bounds.size.width
if offSet == totalPossibleOffset {
//offSet = 0 // come back to the first image after the last image
//timer.invalidate()
}
else {
offSet += self.view.bounds.size.width
}
DispatchQueue.main.async() {
UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.scrollView.contentOffset.x = CGFloat(self.offSet)
}, completion: nil)
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(page)
self.offSet = page * scrollView.frame.size.width // this updates offset value so that automatic scroll begins from the image you arrived at manually
}
此外,我还有第二个问题:当用户手动滑动到其他用户时,如何再次启动计时器间隔。现在,当用户在4秒之前滑到另一张幻灯片时(因为4秒是滑到另一张幻灯片所需的时间),比方说2秒,他将在4-2 =2秒后滑到下一页,而不是预期的4秒。
发布于 2018-08-29 14:52:47
我认为你应该添加一些旗帜,比如
var isSlideTouched = false
在手势识别器中添加
isSlideTouched = true
以及autoScroll()中的一些代码
@objc func autoScroll() {
if isSlideTouched {
isSlideTouched = false
return
}
https://stackoverflow.com/questions/52070135
复制相似问题