3
更新我已經按以下步驟進行的建議,添加路徑,以一個陣列並拉到一個CGLayer。但是,當繪製大量路徑時,性能確實很慢。我犯過什麼錯誤嗎?iOS的石英繪圖應用
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint velocity = [pan velocityInView:self];
previousPoint2 = previousPoint1;
previousPoint1 = currentPoint;
currentPoint = [pan locationInView:self];
float velocityMagnitude = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y);
float clampedVelocityMagnitude = clamp(VELOCITY_CLAMP_MIN, VELOCITY_CLAMP_MAX, velocityMagnitude);
float normalizedVelocity = (clampedVelocityMagnitude - VELOCITY_CLAMP_MIN)/(VELOCITY_CLAMP_MAX - VELOCITY_CLAMP_MIN);
float lowPassFilterAlpha = STROKE_WIDTH_SMOOTHING;
float newThickness = (STROKE_WIDTH_MAX - STROKE_WIDTH_MIN) * normalizedVelocity + STROKE_WIDTH_MIN;
self.lineWidth = self.lineWidth * lowPassFilterAlpha + newThickness * (1 - lowPassFilterAlpha);
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef subpath = CGPathCreateMutable();
CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(subpath);
Path *path = [[Path alloc] init];
path.width = self.lineWidth;
path.color = [UIColor redColor];
path.bezierPath = [UIBezierPath bezierPathWithCGPath:subpath];
[pathArray addObject:path];
CGPathRelease(subpath);
CGRect drawBox = bounds;
drawBox.origin.x -= self.lineWidth * 2.0;
drawBox.origin.y -= self.lineWidth * 2.0;
drawBox.size.width += self.lineWidth * 4.0;
drawBox.size.height += self.lineWidth * 4.0;
[self setNeedsDisplayInRect:drawBox];
if (pan.state == UIGestureRecognizerStateEnded | pan.state == UIGestureRecognizerStateCancelled)
{
self.lineWidth = STROKE_WIDTH_MIN;
}
}
- (void)drawRect:(CGRect)rect {
[self.backgroundColor set];
UIRectFill(rect);
CGContextRef context = UIGraphicsGetCurrentContext();
if(layer == nil)
{
float scale = [UIScreen mainScreen].scale;
CGRect bounds = CGRectMake(0, 0, rect.size.width * scale, rect.size.height * scale);
layer = CGLayerCreateWithContext(context, bounds.size, NULL);
layerContext = CGLayerGetContext(layer);
}
for(int i = 0; i < [pathArray count]; i++)
{
Path *path = [pathArray objectAtIndex:i];
UIBezierPath *bezierPath = path.bezierPath;
CGContextAddPath(layerContext, bezierPath.CGPath);
CGContextSetLineWidth(layerContext, path.width);
CGContextSetStrokeColorWithColor(layerContext, path.color.CGColor);
CGContextSetLineCap(layerContext, kCGLineCapRound);
CGContextStrokePath(layerContext);
}
CGContextDrawLayerAtPoint(context, CGPointZero, layer);
self.empty = NO;
}
我正在使用下面的代碼根據用戶的平移手勢的速度繪製可變寬度的線條。它工作正常,但是當我在前面繪製的線附近或之上繪製時,其線寬將更改爲與當前手勢和線的速度相匹配。我怎樣才能確保畫出的線條保持原來繪製的厚度?
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint velocity = [pan velocityInView:self];
previousPoint2 = previousPoint1;
previousPoint1 = currentPoint;
currentPoint = [pan locationInView:self];
float velocityMagnitude = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y);
float clampedVelocityMagnitude = clamp(VELOCITY_CLAMP_MIN, VELOCITY_CLAMP_MAX, velocityMagnitude);
float normalizedVelocity = (clampedVelocityMagnitude - VELOCITY_CLAMP_MIN)/(VELOCITY_CLAMP_MAX - VELOCITY_CLAMP_MIN);
float lowPassFilterAlpha = STROKE_WIDTH_SMOOTHING;
float newThickness = (STROKE_WIDTH_MAX - STROKE_WIDTH_MIN) * normalizedVelocity + STROKE_WIDTH_MIN;
self.lineWidth = self.lineWidth * lowPassFilterAlpha + newThickness * (1 - lowPassFilterAlpha);
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef subpath = CGPathCreateMutable();
CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(subpath);
CGPathAddPath(path, NULL, subpath);
CGPathRelease(subpath);
CGRect drawBox = bounds;
drawBox.origin.x -= self.lineWidth * 2.0;
drawBox.origin.y -= self.lineWidth * 2.0;
drawBox.size.width += self.lineWidth * 4.0;
drawBox.size.height += self.lineWidth * 4.0;
if (pan.state == UIGestureRecognizerStateEnded | pan.state == UIGestureRecognizerStateCancelled)
{
self.lineWidth = STROKE_WIDTH_MIN;
}
else
{
[self setNeedsDisplayInRect:drawBox];
}
}
- (void)drawRect:(CGRect)rect {
[self.backgroundColor set];
UIRectFill(rect);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);
self.empty = NO;
}
請參閱我的問題更新進一步進展。這看起來是否正確?謝謝 – Simon
正確的做法是繪製圖層和「當前」路徑,而不是所有路徑。該圖層應該包含所有'已保存'的路徑。 – Wain
謝謝Wain,感謝 – Simon