2

我想让用户在 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'方法,我只能得到一个接触点。

4

3 回答 3

3

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

[编辑] 更好的解决方案是在UIEvent控制器收到
– touchesMoved:withEvent: 通知后直接从对象获取触摸。此外,GLPaint 示例代码也可能很有帮助。

于 2009-03-25T06:23:52.587 回答
2

由于 GLPaint 示例代码对于初学者来说可能过于复杂,所以我找到了这个教程。对于大多数初学者来说,学习起来很简单。

于 2009-03-30T05:18:51.377 回答
0

请记住,用手指书写的签名与用书写工具书写的签名不同。您可能需要重新考虑使用签名的目的,以及它是否具有您需要的约束力。

于 2009-03-30T05:51:47.570 回答