codework
codework

Reputation: 103

Enable / Disable Touch Events on UIImageView Object

I've read answers to many questions that dealt with enabling / disabling touch events, but nothing has worked for me, so I'm asking one of my own.

I have a UIImageView object (spot):

// in my view controller header file:
@property (nonatomic, strong) IBOutlet UIImageView *spot;

Then I have code relating to this object:

// in my view controller .m file:
@synthesize spot

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // handle when that spot is touched ... 
}

And that works fine. For example, I can change the image displayed at the spot when the spot is clicked.

First I wanted to see how to disable touch events on the spot so I tried:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

And that works fine. At certain points, depending on what I want to do, I was able to disable all touch events.

Then I added a button to this view controller and I want the button to be clickable ALWAYS, to have a touch event ALWAYS enabled for the button.

So now my approach to disabling touch events won't work because it's too heavy-handed. It wipes out all touch events anywhere in that view.

I want to disable ONLY the touch event on that spot. I tried:

spot.userInteractionEnabled = NO;

But that didn't work. The spot was still clickable. I also tried:

[spot1 setUserInteractionEnabled:NO];

Also didn't work. I'm quite confused as to why those don't work. My question is:

How can I disable touch events on just this one spot, this one UIImageView object?

EDIT: To address the question asked below, in the Interface Builder, in my .xib I have linked the UIImageView object to the property set in my header file. That's its Referencing Outlet.

Upvotes: 0

Views: 3509

Answers (1)

xexe
xexe

Reputation: 346

Why do you want to disable touch for your spot? You can simply skip handling if the touch was from the spot.

 -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
     UITouch *touch = [[event allTouches] anyObject];

     CGPoint touchLocation = [touch locationInView:self.view];
     if (CGRectContainsPoint(spot.frame, touchLocation))
         return;

     if (CGRectContainsPoint(button.frame, touchLocation)){
         //do something
     }
 }

Upvotes: 2

Related Questions