• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:33:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

enter image description here 上图为红点,我喜欢加入红点。

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

   -(void) drawRectCGRect)rect
{

    if([pointarray count]==4)
    {
        float firstpointx= [[pointarray objectAtIndex:0]floatValue];
        float firstpointy= [[pointarray objectAtIndex:1]floatValue];
        float secondpointx= [[pointarray objectAtIndex:2]floatValue];
        float secondpointy= [[pointarray objectAtIndex:3]floatValue];

        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].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);
        [pointarray removeAllObjects];//remove first two points from ur array so that next line is not drawn in continuous with previous line
    }
}
-(void) touchesBeganNSSet *)touches withEventUIEvent *)event
{ 
    pointarray=[[NSMutableArray alloc]init];
    CGPoint curPoint = [[touches anyObject] locationInView:self.view];
    [pointarray addObject:[NSNumber numberWithFloat:curPoint.x]];
    [pointarray addObject:[NSNumber numberWithFloat:curPoint.y]];
    NSLog(@"the point array is %@",pointarray);
    [self.view setNeedsDisplay]; // calls drawRectMethod
}
- (void)touchesMovedNSSet *)touches withEventUIEvent *)event {
}


- (void)touchesEndedNSSet *)touches withEventUIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        imageView.image = nil;
        return;
    }

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        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();

    }

}

在我将我的点放置在这里之后如何连接这两个点,有人可以帮助我!!!



Best Answer-推荐答案


使用下面的代码就可以了:-

- (void)touchesBeganNSSet *)touches withEventUIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource"white" ofType"png"]]];
        return;
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;

}

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

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    currentPoint.y -= 20;

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

    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
    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;

}

欢迎提问..

关于iphone - 在iphone中的两点之间画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8208469/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap