2011-06-27 193 views
1

我使用(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event來處理在UIView上的某些拖動操作。但是,這個UIView有一些UIButtons作爲UIView的子視圖,當用戶觸及UIButton(它們也在UIView之上)時,不調用觸摸方法。如何在觸摸UIButton時通過UIView檢測觸摸?

我需要在任何時候調用UIView中的觸摸方法,並且仍然有UIButtons工作,我該如何實現這一點?

+0

試着接受你以前的答案...... – Aravindhan

回答

3

好的,沒問題,我已經解決了我的問題。

的方式是覆蓋在按鈕觸摸方法也是這樣的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    touchMoved = NO; 
    [[self superview] touchesBegan:touches withEvent:event]; 
    [super touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    touchMoved = YES; 
    [[self superview] touchesMoved:touches withEvent:event]; 
    [super touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!touchMoved) [super touchesEnded:touches withEvent:event]; 
} 

touchMoved變量是爲了跟蹤,如果它是直接觸摸到按鈕或者如果觸摸是爲了拖超級觀點。由於我使用的是UIControlEventTouchUpInside,因此如果我在跟蹤觸摸移動時禁用touchesEnded,它就可以正常工作。

+0

很棒!謝謝!! – DZenBot