2012-09-21 116 views
1

我正在尝试实现绘图功能。目前我使用UIBezierPath:缩放后的平滑线

@interface DrawView : UIView { 
    UIBezierPath *myPath; 
} 
@end 

@implemenation DrawView 

-(id)initWithFrame:(CGRect)frame { 
    //...normal init code 
    myPath = [[UIBezierPath alloc]init]; 
    myPath.lineCapStyle = kCGLineCapRound; 
    myPath.lineJoinStyle = kCGLineJoinRound; 
    myPath.miterLimit = 0; 
    myPath.lineWidth = 10; 
} 

touchesBegan:我跟踪的startPoint和调用[myPath moveToPoint:myTouch locationInView:self];touchesMoved:我打电话[myPath addLineToPoint...]; [self setNeedsDisplay]

我的DrawView是一个UIScrollView的子视图。

我的问题: 当我放大绘图是非常角或像素化。

我想以某种方式重绘图形,使其看起来很漂亮平滑,但我无法弄清楚如何或从何处开始。

我发现这个简单的“解决方案”:

-(void)scrollViewDidEndZooming:(UIScrollView*)scrollView withView:(UIView*)view atScale:(float)scale { 
    view.layer.contentsScale = scale; 
    [view setNeedsDisplay]; 
} 

当我实现这样的图纸将zomming后平滑的方法,但应用程序变得很laggy和缓慢的。在scrollView应用程序的高maximumZoomScale完全膨胀。当我正确理解文档设置contentsScale高于2不是一个好主意,或者以任何方式更改contenscale都不是那么好。

回答

0

我发现我的问题的解决方案(我仍然面临着其他问题图纸...): 在缩小完成我打电话,看起来像这样的方法:

-(void)redrawWithScale:(float)scale { 
    _myPath.lineWidth = _myPath.lineWidth * scale //currently _myPath is a UIBezierPath, with CGPath it should be equal 
    [_myPath applyTransform:CGAffineTransformMakeScale(scale, scale)]; 

    [self setNeedsDisplay]; 

    //or with a CAShapeLayer: 
    _shapeLayer.path = _myPath.CGPath; 

}

编辑:

在我目前的实施中,我使用CAShapeLayer(作为我的UIView的子图层)。 UIBezierPath被设置为shapeLayer的path属性。 调用setNeedsDisplay不是必需的解决方案。

0

查看WWDC 2012视频Optimizing 2D Graphics and Animation Performance获取一些优秀的技巧。我建议在可能的情况下减少或消除透明层,并连接各个线段。

+0

非常感谢发布此链接。它有助于了解石英芯,而不是优化。我必须使用会话视频中的优化步骤。但我认为这不会回答我的缩放问题。非常知道我发现了一些iOS画图的例子,但从来没有放大。 – Sebastian