如何在iPhone屏幕上绘制线条签名?

时间:2009-03-25 02:12:47

标签: iphone objective-c opengl quartz-graphics paint

我想让用户在iPhone屏幕上绘制一个签名,所以我添加了一个UIView的子类,并在其'touchesMoved'方法中添加了一些代码。


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    firstTouch = [touch locationInView:self];

    CGSize mySize = CGSizeMake(5, 5);
    UIGraphicsBeginImageContext(mySize);                                    
    CGContextRef ctx = UIGraphicsGetCurrentContext();                       

    CGContextBeginPath(ctx);                                                
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);                              
    CGContextAddRect(ctx, CGRectMake(0, 0, 5, 5));                          
    CGContextFillPath(ctx);                                                 

    UIImage *redRect = UIGraphicsGetImageFromCurrentImageContext();         
    UIGraphicsEndImageContext();                                            

    UIImageView *redRectView = [[UIImageView alloc] initWithImage:redRect]; 
    redRectView.center = CGPointMake(firstTouch.x, firstTouch.y);                               
    [self addSubview:redRectView];                                      

}

我用小矩形绘制它,结果是一点一点。因为它太丑了,我想用线条绘制签名。但是如何区分firstTouch和lastTouch?如果我只使用'touchesMoved'方法,我只能得到一个触点。

3 个答案:

答案 0 :(得分:3)

根据UIResponder Class Reference,您还需要实施
– touchesBegan:withEvent:
– touchesEnded:withEvent:
实现这些方法后,您应该能够获得足够的数据来实现路径贝塞尔曲线或其他合适的解决方案。

[编辑]或许更好的解决方案是在控制器收到一个后直接从UIEvent对象获取触摸 – touchesMoved:withEvent: 通知。此外,GLPaint sample code也可能有用。

答案 1 :(得分:2)

由于GLPaint示例代码对于初学者来说可能过于复杂,我找到了this tutorial。大多数初学者都很容易学习。

答案 2 :(得分:0)

请记住,用手指书写的签名与使用书写工具书写的签名不同。您可能想要重新考虑您使用签名的内容,以及它是否具有您需要的绑定。

相关问题