iOS:用手指画一条线 [英] IOS: draw a line with your finger

查看:142
本文介绍了iOS:用手指画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在白色视图中用手指画一条线的方法。我想做一个画板,我想开始理解如何用手指画一条简单的线或轨迹。我该怎么办?

I want to know what's the approach to draw a line with a finger in a white view. I want to do an artboard, and I want begin to understand how draw a simple line or a track done with a finger. How can I do it?

推荐答案

我已经了解您的问题。
请参见下面的代码,它将完全为您使用。

I have understood your problem. Please see the bellow code.It will use full for you.

-(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, 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();
 }

这篇关于iOS:用手指画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆