检测UIScrollView是否正在滚动 [英] Detect if UIScrollView is scrolling

查看:97
本文介绍了检测UIScrollView是否正在滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要检测 UIScrollView 是否(不是在什么时候)滚动。

I'm looking to detect if (not when) a UIScrollView is scrolling.

ie

BOOL isScrolling = myscrollview.scrolling;

我将如何实施此功能? .dragging .decelerating setContentOffset:animated:

How would I go about implementing this? .dragging and .decelerating do not apply when setContentOffset:animated: is used.

推荐答案

这可能是一种略微狡猾的方式,但我在我的代码中使用它以前它还没有让我失望。 :)

This may be a slightly dodgy way of doing it, but I've used it in my code before and it hasn't failed me yet. :)

#import <QuartzCore/QuartzCore.h>
---
BOOL isScrolling = [scrollView.layer animationForKey:@"bounds"] != nil;

除了'self.contentOffset'之外,UIScrollView的偏移量存储在bounds.origin中UIView的属性(我想因为它在低级代码中更方便)。当你调用[setContentOffset:animated:]时,它将它作为CABasicAnimation传递给UIScrollView,操纵bounds属性。

In addition to 'self.contentOffset' the offset of a UIScrollView is stored in the "bounds.origin" property of the UIView as well (I guess since it's a bit more convenient in lower-level code). When you call [setContentOffset:animated:], it's passing that off to the UIScrollView as a CABasicAnimation, manipulating the "bounds" property.

当然,这个检查也会返回如果scrollView本身的形状是动画,则为true。在这种情况下,您可能需要实际将边界对象拉出动画([CAAnimation toValue])并进行手动比较以查看origin本身是否正在发生变化。

Granted, this check will also return true if the shape of the scrollView itself is animating. In which case you may need to actually pull the bounds object out of the animation ([CAAnimation toValue]) and do a manual compare to see if "origin" itself is changing at all.

这不是最优雅的解决方案,但我希望能够避免在任何可能的情况下使用代理。特别是如果有问题的类是UIScrollView子类。 :)

It's not the most elegant solution, but I like being able to avoid having to use the delegate wherever possible. Especially if the class in question is a UIScrollView subclass. :)

这篇关于检测UIScrollView是否正在滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆