2011-09-26 85 views
0

在我的程序中有两个视图(scrollView-super,view-sub * 2)。如何在SuperView上获取touchesBegan事件

在我的情况下,两个子视图在scrollView中被子查看。在子视图中调用touchesBegan事件。

如何在scrollView中获取事件?

@interface MyScrollView:UIScrollView 
... 
@implement MyScrollView 
... 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //here is my code 
    //but I can't get event here 
    ... 
} 
-(id)initWithFrame:(CGRect) frame 
{ 
... 
    MyView *view1 = [[MyView alloc] initWithFrame:(0, 0, 320, 240); 
    MyView *view2 = [[Myview alloc] initWithFrame:(0, 240, 320,240); 
    [self addSubview: view1]; 
    [self addSibvoew: view2]; 
... 
} 

@interface MyView:UIView 
... 
@implement MyScrollView 
... 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //break points 
    //here comes event 
} 

回答

1

试试这个代码..

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 

[scroll addGestureRecognizer:singleTap]; 

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)touch{ 
    CGPoint touchPoint=[gesture locationInView:scrollView]; 
    touchPoint = [touch locationInView:self.view]; 
} 
+0

我怎样才能阻止子视图的touchesBegan事件 – bTagTiger

+0

,我怎么能得到touchesMove方法? – bTagTiger

+0

@bTagTiger:你无法在UITapGestureRecognizer中移动触摸。为避免触及子视图,只需禁用子视图的用户交互即可。 – Prajan

0

我建议让你的两个subview全局,然后在superview touchesbegan方法通过触摸和事件处理的子视图。所以somethig like [view1 touchesBegan:touches event:event]; 我还没有测试过,但应该是单向的。

相关问题