2

我使用UISwipeGestureRecognizer和我的覆盖

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

似乎 touchesBegan 和 touchesMoved 一直跟踪触摸,直到识别到 Swipe Gesture 并且不调用 touchesEnded(与 touchesCancelled 相同)。但是我需要滑动手势识别器和 touchesEnded 来完成这项工作,我该怎么做?

4

1 回答 1

10

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

ss

然后您在 View 中检查取消的项目。

ss

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

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

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

- (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

于 2012-08-12T10:50:13.150 回答