gpt4 book ai didi

iphone - 在 UIScrollView 的子类中检测滚动

转载 作者:行者123 更新时间:2023-12-03 18:41:08 24 4
gpt4 key购买 nike

早上好,

我创建了 UIScrollView 的子类,现在我想知道用户何时在我的子类中滚动。为此,我按照以下方式实现了它:

ImageScroller.h

@interface UIImageScroller : UIScrollView <UIScrollViewDelegate> {

ImageScroller.m(在@implementation内)

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

NSLog(@"did scroll");
}

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

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

 - (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 的委托(delegate)时,如下所示 [imageScroller setDelegate:imageScroller]scrollViewDidScroll 在我的 ImageScroller-Subclass 中被触发,但我的 ViewController 中的那个没有被触发,但我需要两者。

有什么解决方案吗?

感谢您提前的答复。

最佳答案

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

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

//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 方法而言,我认为 View Controller 应该负责处理这些方法。

关于iphone - 在 UIScrollView 的子类中检测滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5673982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com