在 UIScrollView 的子类中检测滚动
早上好,
我创建了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在创建 UIScrollView 的子类时遇到了同样的问题,但以这种方式解决了。如上所述,您可以将自己(子类实例)设置为委托,但这就是我所做的。
在子类中,我重写了以下方法
至于其他 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
As far as the other
UIScrollViewDelegate
methods, the View Controller should be responsible for handling those in my opinion.我想你可以尝试设置 self.delegate = self;接收事件。
I think you can try setting self.delegate = self; to receive events.
如果有人正在寻找 Swift 答案,那就这么简单:
If anyone is looking for a Swift answer, it's as simple as this: