我使用UISwipeGestureRecognizer
和我的覆盖
-(void)touchesBegan...,-(void)touchesEnded...,-(void)touchesMoved... methods.
似乎touchesBegan和touchesMoved保持跟踪触摸,直到Swipe Gesture识别并且touchesEnded未被调用(与touchesCancelled相同)。但我需要轻扫手势识别器和触摸器才能完成这项工作,我该怎么办呢?
答案 0 :(得分:10)
首先,将滑动手势识别器从库拖放到视图中。
然后检查项目在视图中取消。
编写代码以响应轻扫手势。
- (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];
}
现在您可以在不取消的情况下移动图像,并且可以滑动屏幕以设置蓝色(成功识别滑动手势)。两者都可以。并且,当触摸结束时,窗口颜色变为红色。
您可以下载此示例项目并运行它: