0

在我的应用程序中,我可以查看用户可以在哪里绘制箭头。它工作得几乎完美。如果我慢慢滑动,我会得到很好的结果。 图片

但是,如果我尝试更快地滑动,我会在某处找到箭头,但不是在行尾。 图片

对不起,由于我的声誉,我不能在这里发布图片。

我如何改进箭头线绘图以绘制无论我刷的速度有多快?

为此,我使用下一个方法 -

-(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;
4

1 回答 1

1

根据我在使用核心图形绘制时的经验,在视图上快速滑动时,每次touchesMoved:调用的点之间的距离会更大,而且我看到您使用的 alastMovePoint可能比您想要的更远,给您带来不想要的结果. 缓慢移动并且每个点之间的距离很小(有时甚至小于 1 点)时不会发生这种情况

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

于 2012-12-26T12:44:39.027 回答