Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I am trying to get some UIViews to register touch events. Per Apple's recommendation, I am overriding UIView's hitTest method. I am using this code, which I copied & pasted from Apple's documentation:

Code:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

// Convert the point to the target view's coordinate system.
// The target view isn't necessarily the immediate subview
CGPoint pointForTargetView = [self.targetView convertPoint:point fromView:self];

if (CGRectContainsPoint(self.targetView.bounds, pointForTargetView)) {

// The target view may have its view hierarchy,
// so call its hitTest method to return the right hit-test view
return [self.targetView hitTest:pointForTargetView withEvent:event];
}

return [super hitTest:point withEvent:event];
}

However, for some reason, Xcode says there's no targetView property. Am I using old code? If so, will someone please post either the right code or a link to it? Thanks!

Edit: I am using the iOS 8.4 SDK.
 
OK, so here's some code that actually works (Note: I did try using the pointInside method; however, it didn't work.)

Code:
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

    BOOL (^predicateBlock)(id evalObject, NSDictionary *bindings) = ^BOOL(id evalObject, NSDictionary *bindings)

    {

        CGRect frame = [evalObject frame];

        return (CGRectContainsPoint(frame, point) && ![evalObject isKindOfClass:[UIImageViewclass]]);

    };

    NSArray *views = [self.subviewsfilteredArrayUsingPredicate:[NSPredicatepredicateWithBlock:predicateBlock]];

   

    return [views firstObject];

}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.