How to get touch on a particular view.
I am using
CGPoint Location = [[touches anyObject] locationInView:s开发者_StackOverflowelf.view ];
but want to trigger the action only if an particular subView is clicked.
How to do this.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
}
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");
}
}
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.
// 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
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
}
}
Did u try
CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];
精彩评论