как определить прикосновение в конкретном виде - PullRequest
6 голосов
/ 04 марта 2011

Как прикоснуться к определенному виду.

Я использую

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

, но хотите инициировать действие, только если щелкнуть конкретное подпредставление.
Как это сделать.

Ответы [ 6 ]

8 голосов
/ 04 марта 2011

Я получил ответ сам ... но спасибо другим, которые помогли мне в этом

вот он

UITouch *touch ;
touch = [[event allTouches] anyObject];


    if ([touch view] == necessarySubView)
{
//Do what ever you want
}
7 голосов
/ 04 марта 2011

Попробуйте это

//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");
    }
}
1 голос
/ 24 января 2017

Вот версия swift3 без мультитача:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first, self.bounds.contains(touch.location(in: self)) {
        // Your code
    }
}
1 голос
/ 17 декабря 2011
// 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);
    }
}

вот так вы можете найти представление или подпредставление затронуто

1 голос
/ 04 марта 2011

Вы должны создать подкласс (или создать категорию) UIView и переопределить

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

где перенаправить сообщение соответствующему делегату.

0 голосов
/ 04 марта 2011

Вы пробовали

CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];
...