process both touch event and gesture recognizer

允我心安 提交于 2020-01-23 12:24:05

问题


I use UISwipeGestureRecognizer and my overwritten

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

It seems that touchesBegan and touchesMoved keep tracking touches untill Swipe Gesture recognized and touchesEnded is not called (same as touchesCancelled). But I need both swipe gesture recognizer and touchesEnded to do the job, how can i do it?


回答1:


At first, drag and drop the Swipe Gesture Recognizer from Libraries into View.

And you check off the item canceled in View.

Write code to respond swipe gesture.

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

Then, write touch delegate 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 image without cancelled, and you can swipe the screen to set color blue (Swipe Gesture is successfully recognized). Both you can. And, when touch ends, the window color changes to red.

You can download this sample project and just run it:

https://github.com/weed/p120812_TouchAndGesture



来源:https://stackoverflow.com/questions/11921252/process-both-touch-event-and-gesture-recognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!