2011-11-27 34 views
5

Tôi muốn biết cách tiếp cận để vẽ một đường thẳng bằng ngón tay ở chế độ xem màu trắng là gì. Tôi muốn làm một bản vẽ, và tôi muốn bắt đầu hiểu cách vẽ một đường đơn giản hoặc một đường đi được thực hiện bằng một ngón tay. Tôi làm nó như thế nào?IOS: vẽ một đường bằng ngón tay của bạn

+1

Bạn nên xem xét việc áp dụng bản demo [GLPaint] (http://developer.apple.com/library/ios/#samplecode/GLPaint/Introduction/Intro.html), từ Apple. Nó sẽ dạy cho bạn những điều cơ bản của việc vẽ một ngón tay bằng cách sử dụng OpenGL ES. – Macmade

+0

Thử UIBezierpath. Hướng dẫn này có thể hữu ích cho bạn. http://soulwithmobiletechnology.blogspot.in/2011/05/uibezierpath-tutorial-for-iphone-sdk-40.html –

+0

Một ví dụ khác có thể được tìm thấy ở đây - bộ điều khiển này cung cấp cho đầu vào của một chữ ký và trả về một hình ảnh. Hơn nữa một ví dụ làm việc được cung cấp: https://github.com/bunchjesse/JBSignatureController –

Trả lời

4

Tôi đã hiểu sự cố của bạn. Vui lòng xem mã dưới đây. Nó sẽ sử dụng đầy đủ cho bạn.

-(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(); 
} 
Các vấn đề liên quan