2012-12-26 53 views
0

在我的應用程序中,我有查看用戶可以繪製箭頭的位置。 它的工作幾乎完美。 如果我慢慢地滑動,我有很好的結果。 Image如何改善使用CoreGraphics繪製箭頭線?

如果我試圖刷快一點,我得到了箭頭,但不是在最後一行。 image

對不起,由於我的名譽,我無法在此發佈圖片。

如何改進箭頭線繪製以便快速繪製我刷卡?

要做到這一點,我使用下一個方法 -

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

mouseSwiped = YES; 



UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 

UIGraphicsBeginImageContext(self.view.frame.size); 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    CGContextClearRect(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)); 

    double slopy, cosy, siny; 
    // Arrow size 
    double length = 10.0; 
    double width = 10.0; 

    slopy = atan2((firstPoint.y - lastMovePoint.y), (firstPoint.x - lastMovePoint.x)); 
    cosy = cos(slopy); 
    siny = sin(slopy); 


    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), firstPoint.x, firstPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
     //here is the tough part - actually drawing the arrows 
    //a total of 6 lines drawn to make the arrow shape 

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastMovePoint.x, lastMovePoint.y); 

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 
          lastMovePoint.x + (length * cosy - (width/2.0 * siny)), 
          lastMovePoint.y + (length * siny + (width/2.0 * cosy))); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 
          lastMovePoint.x + (length * cosy + width/2.0 * siny), 
          lastMovePoint.y - (width/2.0 * cosy - length * siny)); 
    CGContextClosePath(UIGraphicsGetCurrentContext()); 

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.tempDrawImage setAlpha:opacity]; 
    UIGraphicsEndImageContext(); 

    lastMovePoint = currentPoint; 
+0

您可能會考慮將路徑結構移動到其自己的函數中以簡化代碼。你可以在[UIBezierPath'](https://gist.github.com/4146780)上使用[這個類別],這個解釋[在這個答案](http://stackoverflow.com/a/13559449/77567)。 –

回答

1

從我的經驗與核心圖形繪製時,在視圖刷卡快的時候,每次touchesMoved:被稱爲點之間的距離較大,我看到你正在使用的lastMovePoint可能會超出你的想象,給你不想要的結果。緩慢移動時不會出現這種情況,並且每個點之間的距離是最小的(有時甚至不到1點)

我建議的工作只能用firstPointcurrentPoint,計算角度和借鑑只使用箭頭,因爲,當涉及到它時,繪製一條線只需要第一個和最後一個點,用戶之前觸摸的位置並不重要:)

+0

非常感謝!現在它完美了!涼!大! –