Search code examples
iosxcodeviewcoordinatesuitouch

Differentiation between views when using UItouch


I'm currently working on a project in iOS where I want the UItouch command to only send co-ordinates out when it's over a certain "image". I've got the co-ordinates outputting using UItouch but am not able to do this for just one specific area.

To my knowledge, the only way to do this is with views. So I've made a new view within my mainview, and from there I have problems and can't seem to get it working.

Has anyone done this before/can give me any advice on this?

PS - I'm using Xcode 4.

Thanks in advance.


Solution

  • Try this out

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches  anyObject];
    if (touch.view==yourImageView) {
        //Coordinate code
    }
    
    }
    

    EDIT: Or try using the UITapGestureRecognizer

    In your interface add the UIGestureRecognizerDelegate

    @interface ViewController : UIViewController <UIGestureRecognizerDelegate> {
    

    then in your viewDidLoad add this

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod)];
    tapped.delegate=self;
    tapped.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tapped];
    

    then in your viewController add these 2 methods

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        if (touch.view==yourImageView) {
            return YES;
        }
        return NO;
    }
    
    -(void)tapMethod {
        //Coordinate code
    }
    

    And make sure you have [yourImageView setUserInteractionEnabled:YES];