简体   繁体   中英

Detect if user has touched the screen on iOS

I googled several other questions and tutorials but couldn't find an answer for my question. I want to detect if the user has touched/tapped/hold/clicked the screen. I tried with touchesBegan: withEvent: but it is not firing any events.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == mapView_) {
    NSLog(@"Touches began");
} else NSLog(@"Touches began");

}

Is there another way to detect user interaction throught touching the screen?

You have to use UITapGestureRecognizer.

Conform your class to UIGestureRecognizerDelegate protocol.

Instantiate the gesture recognizer. For example, to instantiate a UITapGestureRecognizer, we will do:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];

Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:

- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
    //Code to handle the gesture
}

The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like, UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, etc.

Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and numberOfTouchesRequired.

Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:

[self.imageView addGestureRecognizer:tapGestureRecognizer];

After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, ie the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:

tapGestureRecognizer.delegate = self;

Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won't be called.

Reference - Here

You can subclass UIApplication if you are only interested in whether the user touched the screen or not, and implement this method:

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];
    if (event.type == UIEventTypeTouches) {
         // handling code
    }
}

In this case the main.m file would look like this:

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, @"UIApplicationSubclass", @"AppDelegate");
    }
}

The solution was using UIPanGestureRecogniser .

Here's the code that solved the problems and stopped my headache:

    UIPanGestureRecognizer *tap = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[mapView_ setMultipleTouchEnabled:YES];
[mapView_ setUserInteractionEnabled:YES];
mapView_.gestureRecognizers = @[tap];

And then the selector method:

- (void) handleTapFrom: (UIPanGestureRecognizer*)recogniser {
   NSLog(@"Pin");
}

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