ユーザーが指で絵を描く落書きアプリの作成を検討しており、画面に線を描くいくつかの異なる方法に出会いました。私はどこからでもコードを見てきました:
- (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];
}
に:
マウススワイプ = はい; UITouch *touch = [任意のオブジェクトに触れる]; 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]];
}
最後の実装では、ユーザーがドラッグしているポイントを保存し、ユーザーが描くときに線を描きます。ユーザーがアプリを操作している間に多くの描画が行われているため、オーバーヘッドが多く、集中的なものになると思います。
だから私の質問は、描画を行うためのベストプラクティス/ベストな方法はありますか? Apple は特定の方法を他の方法よりも好んでいますか? また、それぞれの方法の利点/欠点は何ですか?