扩展UIScrollView和监视滚动事件 [英] Extending UIScrollView and Monitoring Scroll Events

查看:491
本文介绍了扩展UIScrollView和监视滚动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone应用中实现一些自定义,可重用和高效的滚动行为,因此我使用自定义控件扩展了 UIScrollView ,并希望跟踪滚动移动。

I'm wanting to implement some custom, reusable and efficient scroll behavior in an iPhone app so I'm extending UIScrollView with my custom control and wish to track scroll movements.

现在我知道我可以将自定义控件分配为 UIScrollViewDelegate ,并在内部响应 scrollViewDidScroll 调用,对我来说感觉正确(我可能是错的)。

Now I'm aware that I could probably assign my custom control as a UIScrollViewDelegate and internally respond to scrollViewDidScroll calls but this doesn't feel correct to me (I may be wrong).

它不会感觉正确,因为委托是针对应用程序特定的UI逻辑和控件应该是一个级别以上。这也意味着,如果应用程序类将自己分配为一个代理,似乎效率低下,我就需要中继代理调用。

It doesn't feel correct as the delegate is aimed at application specific UI logic and controls should be a level above this. It would also mean I'd need to relay delegate calls out if an application class assigned itself as a delegate too which seems inefficient.

作为 UIScrollView 我希望能够覆盖触发 scrollViewDidScroll 委托调用的方法,或者给予对模板方法的访问权限,或者监听滚动事件,但我可以'看到任何此类选项。

As a direct descendant of UIScrollView I'd expect to be able to override the method that triggers the scrollViewDidScroll delegate call, or be given access to a template method, or listen out for scroll events, but I can't see any such options.

查看 UITableView.h 文件, UITableView 本身作为一个 UISCrollViewDelegate ,所以我想知道它是如何管理这个(我假设它回收单元格,它必须跟踪它们的位置相对于可见界限)。

Looking at the UITableView.h file, UITableView doesn't seem to set itself as a UISCrollViewDelegate so I'm wondering how it manages this (I'm assuming as it recycles cells it must track their position relative to the visible bounds).

我对这个平台很新,所以我可能会缺少一些明显的东西。任何帮助赞赏。

I'm pretty new to this platform so I may be missing something obvious. Any help appreciated.

推荐答案

找到它! (完全是偶然的)

Found it!! (completely by accident)

我已经辞职自己将我的UIScrollView子类作为一个委托分配给super,然后实现 scrollViewDidScroll:委托方法部分方式通过实现我在我的scrollViewDidScroll方法中间有一个异常。一个快速查看堆栈跟踪显示我的委托方法实际上是从 setContentOffset:

I had resigned myself to assign my UIScrollView subclass as a delegate to super then implement the scrollViewDidScroll: delegate method, however part way through implementing I got an exception in the middle of my scrollViewDidScroll method.. One quick look at the stack trace showed me that my delegate method was actually being called from the setContentOffset:

事实证明,如果 重写setContentOffset: ,每次滚动视图移动时都会调用一次。它带有一个方便的contentOffset结构来告诉你滚动视图目前是什么:)

It turns out, if you override setContentOffset: you get a call every time the scroll view moves! and it comes with a handy contentOffset struct to tell you where the scroll view currently is :)

- (void)setContentOffset:(CGPoint)contentOffset
{
    [super setContentOffset:contentOffset];

    NSLog(@"ViewDidScroll: %f, %f", contentOffset.x, contentOffset.y);
}

这篇关于扩展UIScrollView和监视滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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