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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何检测UIScrollView何时完成滚动在Swift中相关的知识,希望对你有一定的参考价值。

从各方面来看,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中。

似乎由Matt(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
}

但我还是没把它放在一起。有帮助吗?

答案

委托方法告诉您何时完成

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

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

func stoppedScrolling() {
    println("Scroll finished")
}
另一答案

如果用户缓慢滚动,则不会调用scrollViewDidEndDecelerating。这是斯威夫特的Ashley Smart asnwear

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
}
另一答案

有一种UIScrollViewDelegate的方法可以用来检测(或更好地说'预测')滚动真的完成时:

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

当滚动真的完成时,UIScrollViewDelegate可用于检测(或更好地说'预测')。

在我的情况下,我使用水平滚动如下(在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
}
另一答案

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

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
}
另一答案

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

以上是关于如何检测UIScrollView何时完成滚动在Swift中的主要内容,如果未能解决你的问题,请参考以下文章

如何检测用户何时尝试滑动“过去的 UIScrollView 内容偏移”?

检测用户何时中断 UIScrollView 动画

如何检测 UITableView 标题何时滚动?

检测用户何时将手指从 UIScrollView 上移开

当动画为假时,如何检测UIScrollView已完成滚动?

在 UIScrollView 的子类中检测滚动