众所周知,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/