0

我在 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];
}

在视图控制器上,

    _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;
        }
    }

它在模拟器上工作正常,但在真实设备上绘图太慢。有什么问题和任何建议?

泰!

4

1 回答 1

0

我解决了。

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

  2. 最好使用 panGesture 识别器而不是 touchesBegin~End。touchesBegin 和 touchesEnd 之间有延迟。

于 2013-02-13T01:10:25.303 回答