2013-02-27 82 views
3

可能的替代方案,我需要在屏幕上绘制多行(在50-75的范围内),目前使用这下面的函数工作正常。这些画线,下面的代码的40-50后,该应用程序明显减慢在我的iPhone 4要优化我试图消除线阴影它帮助,但仍应用程序没有运行,因为我想那样顺利。我需要优化下面的代码,我的第一个想法是与巴纽线图像更换cashapelayers。但是,新的方法应该支持线旋转,具有相同的宽度不同长度的线,并绘制动画(似乎很多关于我做cgaffinetransforms)。任何想法可以帮助我?优化,以CAShapeLayer

+ (CAShapeLayer *) drawLineOnView:(UIView *) view BetweenPoint1:(CGPoint) point1 Point2:(CGPoint) point2 lineWidth:(CGFloat)lineWidth lineColor:(UIColor *) color Animated:(BOOL) animed 
{ 
    CAShapeLayer *lineShape = [CAShapeLayer layer]; 
    CGMutablePathRef linePath = nil; 
    linePath = CGPathCreateMutable(); 

    //lineShape.opacity = 0.6; 
    lineShape.lineWidth = lineWidth; 
    lineShape.lineCap = kCALineCapRound; 

    if(color==nil) color = [UIColor orangeColor]; //Default value 
    lineShape.shadowColor = [color CGColor]; 
    lineShape.shadowOpacity = 1.0; 
    lineShape.shadowRadius = 5.0; 
    lineShape.strokeColor = [color CGColor]; 

    CGPathMoveToPoint(linePath, NULL, point1.x, point1.y); 
    CGPathAddLineToPoint(linePath, NULL, point2.x, point2.y); 

    if(animed) 
    { 
     CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
     pathAnimation.duration = 1.0; 
     pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
     pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 
     [lineShape addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 
    } 

    lineShape.path = linePath; 
    CGPathRelease(linePath); 
    [view.layer addSublayer:lineShape]; 

    return lineShape; 
} 

部分解决(优化永不结束)

我打破了我的线绘图功能分为2个互补部分并绘制多条线到所述一个形状层,而不是创建新的层各一次。如果不是很好,它会更好。以下是更新后的代码:

+ (CAShapeLayer *) createNewShapeLayerForDrawingLinesOnView:(UIView *) view lineWidth:(CGFloat)lineWidth lineColor:(UIColor *) color 
{ 
    CAShapeLayer *lineShape = [CAShapeLayer layer]; 

    //lineShape.opacity = 0.6; 
    lineShape.lineWidth = lineWidth; 
    lineShape.lineCap = kCALineCapRound; 

    if(color==nil) color = [UIColor orangeColor]; //Default value 
    lineShape.shadowColor = [color CGColor]; 
    lineShape.shadowOpacity = 1.0; 
    lineShape.shadowRadius = 5.0; 
    lineShape.strokeColor = [color CGColor]; 

    [view.layer addSublayer:lineShape]; 
    return lineShape; 
} 

+ (void) addNewLineToShapeLayer:(CAShapeLayer *) shapeLayer BetweenPoint1:(CGPoint) point1 Point2:(CGPoint) point2 
{ 
    CGMutablePathRef combinedPath = CGPathCreateMutableCopy(shapeLayer.path); 

    CGMutablePathRef linePath = CGPathCreateMutable(); 

    CGPathMoveToPoint(linePath, NULL, point1.x, point1.y); 
    CGPathAddLineToPoint(linePath, NULL, point2.x, point2.y); 

    //No paths drawn before 
    if(combinedPath == NULL) 
    { 
     combinedPath = linePath; 
    } 
    else 
    { 
     CGPathAddPath(combinedPath, NULL, linePath); 
    } 

    shapeLayer.path = combinedPath; 
    CGPathRelease(linePath); 
} 

回答

4

虽然我明白要创建多个图层,这将是更有效的将所有线画成一个,并从那里管理行的列表的动画和旋转。您可以在形状层像(标有无漏码“......”)合并路径做到这一点:

CGMutablePathRef combinedPath = CGPathCreateMutableCopy(path.CGPath); 
for(...) 
    CGPathAddPath(combinedPath, NULL, [self makeNewPathFrom:...].CGPath); 
myLayer.path = combinedPath; 

再快,你可以画线的名单,直接到graphics context of a CALayer。这个例子的视图的drawRect:方法是未经测试,但应该给你一个想法:

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetLineWidth(context, lineWidth); 
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); //red 

for(MyLine *line in lines) 
{ 
    CGContextMoveToPoint(context, point1.x, point1.y); 
    CGContextAddLineToPoint(context, point2.x, point2.y); 
} 

如果您需要进一步的优化,你应该考虑的OpenGL。

+0

我想提请它更优化到图形上下文如你所说,你可以举一个简单的伪代码为或链接跟随 – guenis 2013-02-27 00:33:51

+0

肯定。我会充实一点答案。 – 2013-02-27 14:43:59

1

你绝对不希望 75层,每个都有自己的线路。你确定你不能在单一层中绘制一条更复杂的路径吗?

+0

不,我不想要或需要的,我只是不知道如何做到这一点更加优化。 – guenis 2013-02-27 00:35:59

+0

绘制在网元层的一切吗?或者使用分析器?你不给我们,我们需要帮助你的信息。 – 2013-02-27 03:51:47