如何在UIScrollView中检测触摸事件?

时间:2012-01-08 03:13:07

标签: cocoa-touch

在我的程序中,我想要在触摸时检测滚动视图中的图像。我曾在网上看到有些代码可以帮助解决问题 在子类.h文件中:

@interface AllScrollView : UIScrollView 
{
}

@end 

在子类.m文件中:

#import "AllScrollView.h"

@implementation AllScrollView

- (id)initWithFrame:(CGRect)frame 
{
  return [super initWithFrame:frame];
}

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{   
  if (!self.dragging) 
    [self.nextResponder touchesEnded: touches withEvent:event]; 
  else
    [super touchesEnded: touches withEvent: event];
}

@end

然后我在主类上更改方法- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event。然而,没有任何事情发生,我无法检测到触摸。我错过了什么吗?有人能提供更详细的解决方案吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

我使用滚动视图Gesture Recognizers,它们工作正常。在你的情况下,我会选择一个单击识别器,如下所示:

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
    initWithTarget:self action:@selector(handleSingleTap:)];
singleFingerDTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleFingerTap];


- (IBAction)handleSingleTap:(UIGestureRecognizer *)sender {
    NSLog(@"Single tap detected");
}