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

标签 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 中的解决方案。

看来,由 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
}

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

最佳答案

委托(delegate)方法告诉你何时完成

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

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

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

关于ios - 如何检测 UIScrollView 何时在 Swift 中完成滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30582617/

相关文章:

ios - 由于该应用程序崩溃,字典获得零值。展开可选值时意外发现 nil

ios - 如何在 iOS Swift 项目中实现向下箭头/插入符号(如在 iOS 9 音乐应用程序中)?

ios - 如何使用 iOS 设备制作 Distribution Build?

ios - 我应该快速使用 GCD 还是 NSOperationQueue

swift - 如何在 NSMenu 中放置水平 slider (Swift 3、Xcode 8)

ios - 在 UITableView 滚动时在 MKMapView 上放置图钉,从特定屏幕位置的单元格获取坐标。制作引脚 "dance."

ios - ios 中 USDZ 文件位于我的头顶

swift - 如何修复 Xcode 10.0 调试器,它没有完全运行?

ios - 如何使用 Vertical constraintsWithVisualFormat (SWIFT) 实现 UIScrollView

ios - 我怎样才能使新的 UILabel 的默认颜色为黑色(而不是透明的)