开发者

iPhone - how to track touches and allow button taps at the same time?

开发者 https://www.devze.com 2022-12-27 18:35 出处:网络
I\'m wondering how to track touches anywhere on the iPhone screen and still have UIButtons respond to taps.

I'm wondering how to track touches anywhere on the iPhone screen and still have UIButtons respond to taps.

I subclassed a UIView, made it full screen and the highest view in the hierarchy, and overrode its pointInside:withEvent method. If I return YES, I'm able to track touches a开发者_如何学Pythonnywhere on the screen but the buttons don't respond (likely because the view is instructed to handle and terminate the touch). If I return NO, the touch passes through the view and the buttons respond, but I'm not able to track touches.

Do I need to subclass UIButton or is this possible through the responder chain? What am I doing wrong?

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return NO;
}   

//only works if pointInside:withEvent: returns YES.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"began");    
        [self.nextResponder touchesBegan:touches withEvent:event];
}

//only works if pointInside:withEvent: returns YES.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"end");      
        [self.nextResponder touchesEnded:touches withEvent:event];
}


Rather then having extra view you can subclass your applications main window and track touches (and other events in it)

@interface MyAppWindow : UIWindow
...
@implementation MyAppWindow

- (void)sendEvent:(UIEvent*)event {
    [super sendEvent:event];

    if (event.type == UIEventTypeTouches){
         // Do something here
    }
   return;  
}

Then set your application window type to MyAppWindow (I did that in MainWindow.xib in IB)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号