Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nimrod wrote:</p> <blockquote> <p>somewhere set the eventInterceptDelegate to a view controller that you want to intercept events</p> </blockquote> <p>I didn't immediately understand this statement. For the benefit of anyone else who had the same problem as me, the way I did it was by adding the following code to my UIView subclass which must detect touches.</p> <pre><code>- (void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Register to receive touch events MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *) [[UIApplication sharedApplication] delegate]; EventInterceptWindow *window = (EventInterceptWindow *) appDelegate.window; window.eventInterceptDelegate = self; } - (void) viewWillDisappear:(BOOL) animated { // Deregister from receiving touch events MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *) [[UIApplication sharedApplication] delegate]; EventInterceptWindow *window = (EventInterceptWindow *) appDelegate.window; window.eventInterceptDelegate = nil; [super viewWillDisappear:animated]; } - (BOOL) interceptEvent:(UIEvent *) event { NSLog(@"interceptEvent is being called..."); return NO; } </code></pre> <p><br/> This version of <code>interceptEvent:</code> is a simple implementation of pinch-to-zoom detection. NB. Some code was taken from Beginning iPhone 3 Development by Apress.</p> <pre><code>CGFloat initialDistance; - (BOOL) interceptEvent:(UIEvent *) event { NSSet *touches = [event allTouches]; // Give up if user wasn't using two fingers if([touches count] != 2) return NO; UITouchPhase phase = ((UITouch *) [touches anyObject]).phase; CGPoint firstPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self.view]; CGPoint secondPoint = [[[touches allObjects] objectAtIndex:1] locationInView:self.view]; CGFloat deltaX = secondPoint.x - firstPoint.x; CGFloat deltaY = secondPoint.y - firstPoint.y; CGFloat distance = sqrt(deltaX*deltaX + deltaY*deltaY); if(phase == UITouchPhaseBegan) { initialDistance = distance; } else if(phase == UITouchPhaseMoved) { CGFloat currentDistance = distance; if(initialDistance == 0) initialDistance = currentDistance; else if(currentDistance - initialDistance &gt; kMinimumPinchDelta) NSLog(@"Zoom in"); else if(initialDistance - currentDistance &gt; kMinimumPinchDelta) NSLog(@"Zoom out"); } else if(phase == UITouchPhaseEnded) { initialDistance = 0; } return YES; } </code></pre> <p><br/> Edit: While this code worked 100% fine in the iPhone simulator, when I ran it on an iPhone device I encountered strange bugs related to the table scrolling. If this also happens to you, then force the <code>interceptEvent:</code> method to return NO in all cases. This means that the superclass will also process the touch event, but fortunately this did not break my code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload