2013-03-04 56 views
0

我正在探索制作一个涂鸦应用程序,用户在其中用手指绘制图片,并且我遇到了几种不同的将线条绘制到屏幕上的方法。iOS屏幕绘图的最佳实践

- (void)drawRect:(CGRect)rect // (5) 
{ 
    [[UIColor blackColor] setStroke]; 
    [path stroke]; 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path moveToPoint:p]; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path addLineToPoint:p]; // (4) 
    [self setNeedsDisplay]; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesMoved:touches withEvent:event]; 
} 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesEnded:touches withEvent:event]; 
} 

到:我从看到代码的任何地方

mouseSwiped = YES; UITouch * touch = [touch anyObject]; CGPoint currentPoint = [touch locationInView:self.view];

UIGraphicsBeginImageContext(self.view.frame.size); 
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
[self.tempDrawImage setAlpha:opacity]; 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 

和最后一个方法(一个我想出了至少最有意义对我来说)

- (void) viewDidLoad{ 
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(drawImage:)]; 
[self.view addGestureRecognizer:pan]; 
pan.delegate = self; 
paths = [[NSMutableArray alloc]init]; 
} 
- (void) drawImage:(UIPanGestureRecognizer*)pan{ 
CGPoint point = [pan translationInView:self.view]; 

[paths addObject:[NSValue valueWithCGPoint:point]]; 
} 

在最后实现,我会存储用户沿拖动点和画一条线。我觉得这样会有很多开销,但由于在用户与应用程序进行交互时有很多绘图正在进行。

所以我的问题是,有最佳做法/最好的方式来绘图?苹果公司是否比其他公司更喜欢这种方式,每种方式都有哪些优点/缺点?

回答