gpt4 book ai didi

iOS 屏幕绘图最佳实践

转载 作者:行者123 更新时间:2023-11-28 22:38:34 24 4
gpt4 key购买 nike

我正在探索制作一个涂鸦应用程序,用户可以在其中用手指画图,我遇到了几种在屏幕上画线的不同方法。我在任何地方都看到过代码:

- (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 = [触摸任何对象]; 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 是否更喜欢一种特定的方式,每种方式的优点/缺点是什么?

最佳答案

更好的方法是使用用户触摸点作为 UIBezierPath 上的点。请参阅第 233 节 WWDC 2012... http://developer.apple.com/itunes/?destination=adc.apple.com.16351493766

关于iOS 屏幕绘图最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com