2011-11-27 11 views
5

Quiero saber cuál es el enfoque para dibujar una línea con un dedo en una vista blanca. Quiero hacer una mesa de trabajo, y quiero comenzar a entender cómo dibujar una línea simple o una pista hecha con un dedo. ¿Cómo puedo hacerlo?IOS: trace una línea con su dedo

+1

Usted debe echar un vistazo a la aplicación de demostración [GLPaint] (http://developer.apple.com/library/ios/#samplecode/GLPaint/Introduction/Intro.html), de Apple. Le enseñará los conceptos básicos de la pintura con un solo dedo con OpenGL ES. – Macmade

+0

Pruebe UIBezierpath. Este tutorial puede ser útil para usted. http://soulwithmobiletechnology.blogspot.in/2011/05/uibezierpath-tutorial-for-iphone-sdk-40.html –

+0

Otro buen ejemplo se puede encontrar aquí: este controlador proporciona la entrada de una firma y devuelve una imagen. Además, se proporciona un ejemplo de trabajo: https://github.com/bunchjesse/JBSignatureController –

Respuesta

4

He entendido su problema. Por favor, vea el código de abajo. Utilizará lleno para usted.

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