Handle both touch event and gesture recognizer

I am using UISwipeGestureRecognizer and my rewritten

 -(void)touchesBegan...,-(void)touchesEnded...,-(void)touchesMoved... methods. 

TouchsBegan and touchsMoved seem to track touches until Swipe Gesture is recognized and touched Ended (same as touchsCancelled). But I need to both recognize gesture recognition and touch them in order to do this job, how can I do it?

+2
objective-c uiswipegesturerecognizer
source share
1 answer

First, drag the Swipe Gesture Recognizer pointer from the Library into the View.

ss

And you mark the item canceled in the view .

ss

Enter the code to respond with a swipe gesture.

 - (IBAction)swipe:(id)sender { v.backgroundColor = [UIColor blueColor]; } 

Then write the delegate touch method.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* touch = [touches anyObject]; CGPoint pt = [touch locationInView:self]; layer.frame = CGRectMake(pt.x, pt.y, 100, 100); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* touch = [touches anyObject]; CGPoint pt = [touch locationInView:self]; layer.frame = CGRectMake(pt.x, pt.y, 100, 100); } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { layer.frame = CGRectMake(0, 0, 100, 100); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { self.backgroundColor = [UIColor redColor]; } 

Now you can move the image without canceling, and you can scroll the screen to set the color to blue (the Swipe Gesture sign was successfully recognized). And you can. And when the touch ends, the window color changes to red.

ss

You can download this sample project and simply run it:

https://github.com/weed/p120812_TouchAndGesture

+10
source share

All Articles