2013-02-27 63 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