2011-11-27 74 views
5

我想知道用白色的手指畫一條線的方法是什麼。我想做一個畫板,我想開始理解如何繪製簡單的線條或用手指完成的軌跡。我該怎麼做?IOS:用你的手指畫一條線

+1

你應該看看[GLPaint(http://developer.apple.com/library/ios/#samplecode/GLPaint/Introduction/Intro.html)演示應用程序,從蘋果。它會教你使用OpenGL ES進行單指畫的基礎知識。 – Macmade

+0

嘗試UIBezierpath。本教程可能對您有所幫助。 http://soulwithmobiletechnology.blogspot.in/2011/05/uibezierpath-tutorial-for-iphone-sdk-40.html –

+0

另一個很好的例子可以在這裏找到 - 這個控制器提供了一個簽名的輸入並返回一個圖像。此外還提供了一個工作示例:https://github.com/bunchjesse/JBSignatureController –

回答

4

我已經理解你的問題。 請參閱以下代碼。它將爲您充分使用。

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