问题
How to get touch on a particular view.
I am using
CGPoint Location = [[touches anyObject] locationInView:self.view ];
but want to trigger the action only if an particular subView is clicked.
How to do this.
回答1:
I got the answer myself...but thanks other which helped me on this
here it is
UITouch *touch ;
touch = [[event allTouches] anyObject];
if ([touch view] == necessarySubView)
{
//Do what ever you want
}
回答2:
Try this
//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");
}
}
回答3:
You should create a subclass (or create a category) of UIView and override the
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
where redirect the message to the appropriate delegate.
回答4:
// 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 find view or subview is touched
回答5:
Heres 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
}
}
回答6:
Did u try
CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];
来源:https://stackoverflow.com/questions/5192785/how-identify-touch-in-a-particular-view