我正在制作一个简单的笔记/绘画应用程序.我有一个像纸/画布一样工作的视图控制器.在其中我想实现UISwipeGestureRecognizer,例如,当用户快速想要显示菜单时他或她可以向上滑动而不是点击我的“编辑”按钮,或者当他/她想要更换纸张时可能向左或向右滑动注意.
问题是,当用户滑动(用两根手指)时,应用程序也会在同一方向上绘制一条短线.
我已经实现了这样的UISwipeGestureRecognizer:
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
recognizer.numberOfTouchesRequired = 2;
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
// Delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
// Helper method for handeling swipes
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
[self enableEditingMode];
}
}
绘制机制在同一控制器的以下方法中处理:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
看起来上面的三种方法在我的UIGestureRecognizer之前被调用了.我试图创建一个BOOL变量,如果布尔变量为YES,则禁用绘制代码.但这没效果.关于如何解决这个问题的任何其他想法都会很棒!
注意:我正在模拟器中进行滑动测试.也许那影响呢?
最佳答案 你需要确保你也实施
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
然后,您可以撤消触摸.