菜鸟教程小白 发表于 2022-12-11 20:33:19

iphone - 在iphone中的两点之间画线?


                                            <p><p> <img src="/image/TIeaE.png" alt="enter image description here"/>
上图为红点,我喜欢加入红点。</p>

<p>我喜欢在两点之间画一条线,我在 ImageView 中有图像,我喜欢标记图像的某些部分以指示该点,使用触摸事件放置点</p>

<pre><code>   -(void) drawRect:(CGRect)rect
{

    if(==4)
    {
      float firstpointx= [floatValue];
      float firstpointy= [floatValue];
      float secondpointx= [floatValue];
      float secondpointy= [floatValue];

      CGContextRef ctx = UIGraphicsGetCurrentContext();
      CGContextSetStrokeColorWithColor(ctx, .CGColor);
      CGContextSetLineWidth(ctx, 2.0);
      CGContextMoveToPoint(ctx, firstpointx, firstpointy);///move to ur first dot
      CGContextAddLineToPoint(ctx, secondpointx, secondpointy);//add line from first dot to second dot
      CGContextSetLineCap(ctx, kCGLineCapRound);
      CGContextStrokePath(ctx);
      ;//remove first two points from ur array so that next line is not drawn in continuous with previous line
    }
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    pointarray=[init];
    CGPoint curPoint = [ locationInView:self.view];
    ];
    ];
    NSLog(@&#34;the point array is %@&#34;,pointarray);
    ; // calls drawRectMethod
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}


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

    UITouch *touch = ;

    if ( == 2) {
      imageView.image = nil;
      return;
    }

    if(!mouseSwiped) {
      UIGraphicsBeginImageContext(self.view.frame.size);
      ;
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      CGContextFlush(UIGraphicsGetCurrentContext());
      imageView.image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

    }

}
</code></pre>

<p>在我将我的点放置在这里之后如何连接这两个点,有人可以帮助我!!!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用下面的代码就可以了:-</p>

<pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = ;

    if ( == 2) {
       pathForResource:@&#34;white&#34; ofType:@&#34;png&#34;]]];
      return;
    }

    lastPoint = ;
    lastPoint.y -= 20;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = ;   
    CGPoint currentPoint = ;
    currentPoint.y -= 20;

    NSLog(@&#34;current Point is x: %d, y: %d&#34;,currentPoint.x,currentPoint.y);

    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    ;
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}
</code></pre>

<p>欢迎提问..</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 在iphone中的两点之间画线?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8208469/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8208469/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 在iphone中的两点之间画线?