在 UIScrollView 的子类中检测滚动

发布于 2024-11-01 20:34:52 字数 1046 浏览 0 评论 0原文

早上好,

我创建了 UIScrollView 的子类,现在我想知道用户何时在我的子类中滚动。为此,我实现了如下所示:

ImageScroller.h

@interface UIImageScroller : UIScrollView <UIScrollViewDelegate> {

ImageScroller.m (在@implementation 内)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    NSLog(@"did scroll");
}

问题是,方法scrollViewDidScroll 似乎没有被触发。 有可能让它发挥作用吗?

我也尝试将委托设置为自己,但它不起作用。

 - (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        self.directionalLockEnabled = YES;
        [self setDelegate:self];
    }
    return self;
}

我将 ScrollView 添加到我的 XIB 文件中,并将类设置为我的 ImageScroller。另外,我还设置了 Fileowner,并在 ViewController 的 .h 文件中使用 UIScrollViewDelegate ,并在 .m 文件中实现方法 rollViewDidScroll 。

当我在 XIB 的 .m 文件代码中设置 ImageScroller 的委托时,例如 [imageScroller setDelegate:imageScroller] scrollViewDidScroll 在我的 ImageScroller-Subclass 中被触发,但我的 ViewController 中的那个没有被触发,但我需要两者。

有什么解决办法吗?

感谢您提前的答复。

Good morning,

I've creates a Subclass of UIScrollView and I now want to know when the user is scrolling in my subclass. For that I implemented it like the following:

ImageScroller.h

@interface UIImageScroller : UIScrollView <UIScrollViewDelegate> {

ImageScroller.m (within the @implementation)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    NSLog(@"did scroll");
}

The problem is, that the method scrollViewDidScroll doesn't seem to get fired.
Is there any possibility to get it to work?

I also tried to set the delegate to it self, but it doesn't work.

 - (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        self.directionalLockEnabled = YES;
        [self setDelegate:self];
    }
    return self;
}

I added a ScrollView to my XIB-File and set the Class if it to my ImageScroller. Also I've set the Fileowner and I'm using the UIScrollViewDelegate in the .h-File of the ViewController as well as implementing the Method scrollViewDidScroll in the .m-file.

When I set the delegate of my ImageScroller in the code of the .m-file from the XIB like
[imageScroller setDelegate:imageScroller]
the scrollViewDidScroll is fired in my ImageScroller-Subclass, but the one in my ViewController isn't fired, but I need both.

Any solutions for that?

Thanks for your answers in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

善良天后 2024-11-08 20:34:52

我在创建 UIScrollView 的子类时遇到了同样的问题,但以这种方式解决了。如上所述,您可以将自己(子类实例)设置为委托,但这就是我所做的。

在子类中,我重写了以下方法

//Override this to detect when the user is scrolling, this is also triggered during view
//layout.  Here you can check your deceleration rate and determine when the scrolling has
//grinded to a halt.
-(void)setContentOffset:(CGPoint)contentOffset

//Override this to detect when the view has been resized (just a handy override)
-(void)setFrame:(CGRect)frame;

至于其他 UIScrollViewDelegate 方法,我认为视图控制器应该负责处理这些方法。

I ran into the same problem creating a subclass of the UIScrollView but solved it this way. As mentioned above, you can set yourself (subclass instance) as the delegate however this is what I did.

In the sub class I overrode the following methods

//Override this to detect when the user is scrolling, this is also triggered during view
//layout.  Here you can check your deceleration rate and determine when the scrolling has
//grinded to a halt.
-(void)setContentOffset:(CGPoint)contentOffset

//Override this to detect when the view has been resized (just a handy override)
-(void)setFrame:(CGRect)frame;

As far as the other UIScrollViewDelegate methods, the View Controller should be responsible for handling those in my opinion.

苏辞 2024-11-08 20:34:52

我想你可以尝试设置 self.delegate = self;接收事件。

I think you can try setting self.delegate = self; to receive events.

嘿哥们儿 2024-11-08 20:34:52

如果有人正在寻找 Swift 答案,那就这么简单:

override var contentOffset: CGPoint {
    didSet {
        if contentOffset != oldValue {
            //same as scrollViewDidScroll
        }
    }
}

If anyone is looking for a Swift answer, it's as simple as this:

override var contentOffset: CGPoint {
    didSet {
        if contentOffset != oldValue {
            //same as scrollViewDidScroll
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文