【问题标题】:How to draw signature with lines on iPhone screen?如何在 iPhone 屏幕上用线条绘制签名?
【发布时间】:2009-03-25 02:12:47
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 感谢您的提示。签名仅用作我的应用程序的附加功能。这不是识别用户的唯一方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-18
        • 2021-06-15
        • 1970-01-01
        • 2018-11-22
        相关资源
        最近更新 更多