加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

IOS:用手指画一条线

发布时间:2020-12-15 01:48:51 所属栏目:百科 来源:网络整理
导读:我想知道在白色视图中用手指画线的方法是什么.我想做一个画板,我想开始理解用手指画出一条简单的线条或一条轨道.我该怎么做? 解决方法 我理解你的问题. 请参阅下面的代码.它将为您使用. -(void)intializeDrawImage{ drawImage = [[UIImageView alloc]initWi
我想知道在白色视图中用手指画线的方法是什么.我想做一个画板,我想开始理解用手指画出一条简单的线条或一条轨道.我该怎么做?

解决方法

我理解你的问题.
请参阅下面的代码.它将为您使用.

-(void)intializeDrawImage
{
    drawImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,100,320,320)];
   [drawImage setBackgroundColor:[UIColor purpleColor]];
   [drawImage setUserInteractionEnabled:YES];
   [self.view addSubview:drawImage];
}
 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:drawImage];
    startPoint = p;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:drawImage];
    [self drawLineFrom:startPoint endPoint:p];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

-(void)drawLineFrom:(CGPoint)from endPoint:(CGPoint)to
{
    drawImage.image = [UIImage imageNamed:@""];

    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0,drawImage.frame.size.width,drawImage.frame.size.height)];
    [[UIColor greenColor] set];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5.0f);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(),from.x,from.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),to.x,to.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读