2011-06-27 155 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