How to identify a touch in a certain way

How to access a specific view.

I use

CGPoint Location = [[touches anyObject] locationInView:self.view ]; 

but you want to activate the action only if a specific subView button is clicked.
How to do it.

+6
iphone uitouch
source share
6 answers

I got the answer myself ... but thanks to others who helped me on this

here

 UITouch *touch ; touch = [[event allTouches] anyObject]; if ([touch view] == necessarySubView) { //Do what ever you want } 
+8
source share

try it

 //here enable the touch - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // get touch event UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) { //Your logic NSLog(@" touched"); } } 
+7
source share

You must subclass (or create a category) UIView and override

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

where the redirection of the message corresponds to the corresponding delegate.

+1
source share
 // here Tiles is a separate class inherited from UIView Class - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if ([[touch view] isKindOfClass:[Tiles class]]) { NSLog(@"[touch view].tag = %d", [touch view].tag); } } 

like this, you can see that the view or under view has touched

+1
source share

Has a swift3 version without multitouch:

 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first, self.bounds.contains(touch.location(in: self)) { // Your code } } 
+1
source share

Really and tried

 CGPoint Location = [[touches anyObject] locationInView:necessarySubView ]; 
0
source share

All Articles