处理触摸事件和手势识别器

Mat*_*age 2 objective-c uiswipegesturerecognizer

我使用UISwipeGestureRecognizer和我覆盖

-(void)touchesBegan...,-(void)touchesEnded...,-(void)touchesMoved... methods.
Run Code Online (Sandbox Code Playgroud)

似乎touchesBegan和touchesMoved保持跟踪触摸,直到Swipe Gesture识别并且touchesEnded未被调用(与touchesCancelled相同).但我需要轻扫手势识别器和触摸器才能完成这项工作,我该怎么做呢?

Fee*_*ics 10

首先,将Swipe Gesture Recognizer从Libraries 拖放到View中.

SS

然后检查View中取消的项目.

SS

编写代码以响应滑动手势.

- (IBAction)swipe:(id)sender {
    v.backgroundColor = [UIColor blueColor];
}
Run Code Online (Sandbox Code Playgroud)

然后,编写触摸委托方法.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

现在您可以在不取消的情况下移动图像,并且可以滑动屏幕以设置蓝色(成功识别滑动手势).两者都可以.并且,当触摸结束时,窗口颜色变为红色.

SS

您可以下载此示例项目并运行它:

https://github.com/weed/p120812_TouchAndGesture