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

时间:2012-08-12 09:47:13

标签: objective-c uiswipegesturerecognizer

我使用UISwipeGestureRecognizer和我的覆盖

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

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

1 个答案:

答案 0 :(得分:10)

首先,将滑动手势识别器从库拖放到视图中。

ss

然后检查项目在视图中取消

ss

编写代码以响应轻扫手势。

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

然后,编写touch delegate方法。

- (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];
}

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

ss

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

https://github.com/weed/p120812_TouchAndGesture