2011-09-26 86 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]; 我還沒有測試過,但應該是單向的。

相關問題