如何检测UIScrollView何时完成滚动在Swift中

时间:2015-06-01 20:24:14

标签: ios swift uiscrollview

毕竟,Ashley Smart(How to detect when a UIScrollView has finished scrolling)提出的Obj-C中存在一个很好的解决方案。

-(void)scrollViewDidScroll:(UIScrollView *)sender 
{   
[NSObject cancelPreviousPerformRequestsWithTarget:self];
    //ensure that the end of scroll is fired.
    [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.3]; 

...
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
...
}

但是,我需要一个解决方案,在Swift中。

由马特(dispatch_after - GCD in swift?)提供的优秀延迟函数似乎可能有所帮助。

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

并实施为......

delay(0.4) {
    // do stuff
}

但我仍然没有把它放在一起。有帮助吗?

5 个答案:

答案 0 :(得分:18)

委托方法告诉你完成时间

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    self.stoppedScrolling()
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !decelerate {
        self.stoppedScrolling()
    }
}

func stoppedScrolling() {
    println("Scroll finished")
}

答案 1 :(得分:7)

如果用户正在缓慢滚动,则不会调用func scrollViewDidScroll(_ scrollView: UIScrollView) { NSObject.cancelPreviousPerformRequests(withTarget: self) perform(#selector(UIScrollViewDelegate.scrollViewDidEndScrollingAnimation), with: nil, afterDelay: 0.3) } func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { NSObject.cancelPreviousPerformRequests(withTarget: self) // Call your function here } 。这是斯威夫特的Ashley Smart asnwear

y   x1  x2  x3
2   5   5   9
6   4   2   9
4   2   6   15
7   5   10  6
7   5   10  6
5   4   3   12

答案 2 :(得分:5)

有一种UIScrollViewDelegate的方法可用于检测(或更好地说“预测'”当滚动真的完成时:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
UIScrollViewDelegate

可用于检测(或更好地说&#39;预测&#39;)当滚动真的完成时。

在我的情况下,我使用水平滚动如下(在 Swift 3 中):

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    perform(#selector(self.actionOnFinishedScrolling), with: nil, afterDelay: Double(velocity.x))
}
func actionOnFinishedScrolling() {
    print("scrolling is finished")
    // do what you need
}

答案 3 :(得分:1)

您需要检查用户是否已停止拖动,以及在用户停止拖动后视图是否仍在减速:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if collectionView.isDecelerating == false {
        // Perform whichever function you desire for when scrolling has stopped
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    // Perform whichever function you desire for when scrolling has stopped
}

答案 4 :(得分:0)

Swift版本

注意:您提到的答案是一种可能的解决方案,并且与案例不同

func scrollViewDidScroll(_ scrollView: UIScrollView) {

        // YOUR CODE........
        NSObject.cancelPreviousPerformRequests(withTarget: self)
        self.perform(#selector(scrollViewDidEndScrollingAnimation(_:)), with: scrollView, afterDelay: 0.3)
    }

    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {


        // YOUR CODE ........
        NSObject.cancelPreviousPerformRequests(withTarget: self)
    }