2013-02-12 117 views
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]; 
} 

和一个的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; 
     } 
    } 

它可以在模拟器却可以绘制是一个真正的设备上太慢。什么是错误的和任何建议?

Ty!

回答

0

我解决了。

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

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