IOS:用手指画一条直线

6

我想知道如何用手指在白色视图上画一条线。我想做一个艺术板,想要开始了解如何用手指画一条简单的线或轨迹。我该怎么做?


1
你应该查看苹果公司的GLPaint演示应用程序。它将教你使用OpenGL ES进行单指绘画的基础知识。 - Macmade
另一个好的例子可以在此处找到 - 此控制器提供了签名输入并返回图像。此外,还提供了一个可工作的示例:https://github.com/bunchjesse/JBSignatureController - Konrad Lang
尝试使用UIBezierpath。这个教程可能对你有帮助。http://soulwithmobiletechnology.blogspot.in/2011/05/uibezierpath-tutorial-for-iphone-sdk-40.html - Veera Raj
1个回答

6

我理解了你的问题。 请看下面的代码,它对你有帮助。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接