Pregunta

Quiero que el usuario dibuje una firma en la pantalla del iPhone, por lo que añado una subclase de UIView y añadir algo de código a su método '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];                                      

}

Estoy dibujando con pequeños rectángulos y resulta ser punto a punto. Ya que es demasiado feo, quiero llamar la firma con las líneas. Pero cómo distinguir firstTouch y lastTouch? Si yo sólo uso el método 'touchesMoved', sólo puedo conseguir un punto táctil.

¿Fue útil?

Solución

De acuerdo con la UIResponder Referencia de la Clase , también necesitará implementar
– touchesBegan:withEvent: y
– touchesEnded:withEvent:.
Después de implementar estos métodos, debe ser capaz de obtener datos suficientes para implementar una curva de Bezier encauzamiento u otra solución adecuada.

[ editar ] A tal vez mejor solución sería la de obtener los toques directamente desde el objeto UIEvent una vez que su controlador recibe una
– touchesMoved:withEvent: notificación. Además, el href="https://developer.apple.com/iphone/library/samplecode/GLPaint/index.html" rel="nofollow noreferrer"> GLPaint código de ejemplo también puede resultar útil.

Otros consejos

Tenga en cuenta que una firma escrita con el dedo va a ser diferente de un escrito con un instrumento de escritura. Es posible que desee volver a pensar en lo que estás utilizando la firma para, y si es tan vinculante como usted necesita que sea.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top