2011-04-15 46 views
6

早上好,检测滚动在子类UIScrollView的

我已经创建的UIScrollView的子类,我现在想,当用户在我的子类滚动就知道了。对于我实现它如下所示:

ImageScroller.h

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

    NSLog(@"did scroll"); 
} 

的问题是(在@implementation内)

@interface UIImageScroller : UIScrollView <UIScrollViewDelegate> { 

ImageScroller.m,该方法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文件中实现Method scrollViewDidScroll。

当我把我的ImageScroller的代表从XIB中的.m文件一样 的代码[imageScroller setDelegate:imageScroller] 的scrollViewDidScroll在我ImageScroller子类解雇,但一个在我的ViewController ISN没有开火,但我需要两个。

任何解决方案?

感谢您提前给出答案。

回答

4

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

+1

我试了一下这样的,但它并没有出于某种原因。 ((self = [super initWithCoder:aDecoder])){ self.directionalLockEnabled = YES;(id)initWithCoder:(NSCoder *)aDecoder { [self setDelegate:self]; } return self; } – 2011-04-15 08:17:47

+0

我添加了一个ScrollView到我的XIB文件并将Class设置为我的ImageScroller。我还设置了Fileowner,并且在ViewController的.h文件中使用UIScrollViewDelegate以及在.m文件中实现Method scrollViewDidScroll。这可能是一个问题吗? – 2011-04-15 08:21:32

+0

我没有得到它第一次:)所以你的委托方法工作,但只在你的委托(滚动视图)?因为只有一个代表,所以事情应该是这样的。我认为你可能会设置委托给你的控制器,然后让它像这样做 ' - (void)scrollViewDidScroll:(UIScrollView *)scrollView {//做为控制器的东西 [self.view scrollViewDidScroll:(UIScrollView *) self.view]; } ' 但我还不确定这是否是问题所在。 – 2011-04-15 08:53:05

13

我遇到了同样的问题,创建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方法,视图控制器应负责处理那些在我看来。

1

如果有人正在寻找一个3雨燕的答案,它是如此简单:

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