用两个手指手势滚动

时间:2013-02-12 07:18:30

标签: objective-c uiscrollview uigesturerecognizer drawrect

我在UIScrollView上有一个绘图视图。

我想要做的是用一根手指画线,然后用两根手指滚动。

图纸视图是通过touchesMoved绘制线条,如下所示。

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch;
    CGPoint lastTouch, currentTouch;

    for (touch in touches)
    {
        lastTouch = [touch previousLocationInView:self];
        currentTouch = [touch locationInView:self];

        CGContextRef ctx = CGLayerGetContext(drawLayer);
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, lastTouch.x, lastTouch.y);
        CGContextAddLineToPoint(ctx, currentTouch.x, currentTouch.y);
        CGContextStrokePath(ctx);
    }

    [self setNeedsDisplay];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGContextDrawLayerInRect(drawContext, self.bounds, drawLayer);
    CGContextClearRect(CGLayerGetContext(drawLayer), self.bounds);
    [self setNeedsDisplay];
}

并在viewController上,

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [_scrollView setContentSize:CGSizeMake(320, 800)];
    [self.view addSubview:_scrollView];

    _drawingView = [[DrawingView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
    [_scrollView addSubview:_drawingView];

    for (UIGestureRecognizer *gestureRecognizer in _scrollView.gestureRecognizers)
    {
        if ([gestureRecognizer  isKindOfClass:[UIPanGestureRecognizer class]])
        {
            UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *) gestureRecognizer;
            panGR.minimumNumberOfTouches = 2;
        }
    }

它在模拟器上运行正常但是在真实设备上绘图太慢了。有什么不对,有什么建议吗?

泰!

1 个答案:

答案 0 :(得分:0)

我解决了。

  1. 不应使用[self setNeedsDisplay]绘制整个屏幕。应绘制一个需要重绘的区域[self setNeedsDisplay withRect:]

  2. 比touchesBegin~End更好地使用panGesture识别器。 touchesBegin和touchesEnd之间存在延迟。