我正在开发一个简单的笔记/绘画应用程序。我有一个像纸张/画布一样的视图控制器。在其中,我想实现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;

这样您就可以撤消接触。

关于iphone - 绘画应用程序中的UISwipeGestureRecognizer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7676108/

10-09 13:23