Как прикоснуться к определенному виду.
Я использую
CGPoint Location = [[touches anyObject] locationInView:self.view ];
, но хотите инициировать действие, только если щелкнуть конкретное подпредставление. Как это сделать.
Я получил ответ сам ... но спасибо другим, которые помогли мне в этом
вот он
UITouch *touch ; touch = [[event allTouches] anyObject]; if ([touch view] == necessarySubView) { //Do what ever you want }
Попробуйте это
//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"); } }
Вот версия swift3 без мультитача:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first, self.bounds.contains(touch.location(in: self)) { // Your code } }
// 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); } }
вот так вы можете найти представление или подпредставление затронуто
Вы должны создать подкласс (или создать категорию) UIView и переопределить
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
где перенаправить сообщение соответствующему делегату.
Вы пробовали
CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];