如何使用CoreGraphics改进箭头线条画?

时间:2012-12-26 12:39:49

标签: objective-c core-graphics

在我的应用程序中,我有查看用户可以绘制箭头的位置。 它几乎完美无缺。 如果我慢慢刷,我有很好的结果。 Image

如果我试图刷得更快,我会把箭头放在某个地方而不是在最后一行。 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;

1 个答案:

答案 0 :(得分:1)

根据我使用核心图形绘制的经验,在视图上快速滑动时,每次调用touchesMoved:时点之间的距离都会更大,我发现您使用的是lastMovePoint可以比你想要的更远,给你不想要的结果。当缓慢移动并且每个点之间的距离最小(有时甚至小于1点)时,不会发生这种情况

我建议只使用firstPointcurrentPoint,计算角度并仅使用它绘制箭头,因为,当涉及到它时,要画一条线,你只需要第一个和最后一点,用户之前触摸的位置无关紧要:)

相关问题