简体   繁体   中英

How to detect any Touch in a UIView?

I have this code and the method is not called, what can I do? I need a delegate?

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

    UITouch *touch = [touches anyObject];

    if ([touch view] == self.view)
    {

    }

}

You are not using this code in your UIView .

This is a delegate of the UIResponder class and UIView inherits itself from UIResponder . Hence this method is called if it is defined in a UIView class and not in the UIViewController .

Moreover, if it being the code in your UIView , you won't need to check the view as it is THAT VIEW . Because if the view takes touches, you are sure that it is the right view :)

Try to add userInteractionEnabled = YES to your UIView.

Or maybe another view is catching the event.

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