过程既触摸事件和手势识别
问题描述:
我用UISwipeGestureRecognizer
和我重写过程既触摸事件和手势识别
-(void)touchesBegan...,-(void)touchesEnded...,-(void)touchesMoved... methods.
看来的touchesBegan和touchesMoved保持跟踪直到触摸滑动手势识别,touchesEnded不叫(同touchesCancelled)。但我需要滑动手势识别器和touchesEnded来完成这项工作,我该怎么做?
答
首先,将滑动手势识别器从库中拖放到视图中。
你检查过在查看取消的项目。
写代码来响应滑动手势。
- (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];
}
现在你可以将图像不取消,你可以滑动屏幕来设置颜色为蓝色(滑动手势识别成功)。你可以。而且,当触摸结束时,窗口颜色变为红色。
您可以下载这个示例项目,只是运行它:
+0
谢谢,这就是我需要的!在我的情况下,我还必须解决“延迟结束”问题。 – Andrea 2014-03-21 12:29:44
你能告诉一些代码,使我们知道我们正在讲的? – sergio 2012-08-12 10:17:09