简体   繁体   中英

How to identify touch on UIView subview

I have a scrollview. Inside the scrollview, I have three subviews, A, B, C. When I click on subview A, I want to get either its tag value or know which view I've clicked. I've gone through many codes and blogs, but couldn't find a solution for it.

    - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {}
    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{} //This function is not working with sub view.

Tried these methods but didn't solve my problem.

Use UITapGestureRecognizer . Add a tap gesture recognizer to each of A, B, C views, set the delegate to the view controller and you will be notified on each tap.

UITapGestureRecognizer* tgrA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[viewA addGestureRecognizer:tgrA];
...

-(void) handleTapGesture:(UIGestureRecognizer *)sender 
{
    //sender.view.tag will give you what you need.
}

More information on tap gesture recognizer here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITapGestureRecognizer_Class/Reference/Reference.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM