2014-05-06 23 views
0

我想提及這篇文章是類似於Continuous drawing in CGContext with drawRect,但它沒有任何代碼片段解決方案,所以再次詢問。保持以前的UIBezierPath筆畫在連續調用drawRect

我正在學習iOS開發應用程序。我正在嘗試使用UIBezierPath製作繪圖應用程序。目前,只要我有新的觸摸和新的UIBezierPath我做以下顯示我以前的UIBezierPath。請讓我知道是否有更好/推薦的方式。我有顏色數組來跟蹤用於繪製每個beizer路徑的顏色,因爲我可以選擇更改每個路徑的顏色。

- (void)drawRect:(CGRect)rect // (4) 
{ 
    UIBezierPath *currentPath; 
    UIColor *currentColor; 
    for (int index=0;index<[self.pathArray count]; index++) 
    { 
     currentPath = [self.pathArray objectAtIndex:index]; 
     currentColor = [self.strokeArray objectAtIndex:index]; 
     [currentColor setStroke]; 
     [currentPath stroke]; 
    } 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 


    UIColor *currentStrokeColor; 
    currentStrokeColor = [self copyColor:self.strokeColor]; 
    self.path = [UIBezierPath bezierPath]; 
    [self.path setLineWidth:2.0]; 
    [self.pathArray addObject:self.path]; 
    [self.strokeArray addObject:currentStrokeColor]; 
    [self.path moveToPoint:p]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [self.path addLineToPoint:p]; // (4) 
    [self setNeedsDisplay]; 
} 
+0

您對此解決方案有任何問題嗎? –

+0

不,但我認爲這是一個有效的解決方案?就像每次我做一個新的筆畫一樣,我會清除草圖並重新繪製所有的草圖。這是否有效?這不會導致很多路徑閃爍嗎? –

+1

現在我明白你的意思了。確實有更好的方法。在每一次繪製線條之後,您可以將整個視圖渲染爲圖像,以便之後將線條繪製在靜態圖像上方。看到這個問題:http://stackoverflow.com/questions/19362670/drawing-application-for-ios-performance-issue/19365625#19365625 –

回答

1

繪製一條路徑後,您可以將其渲染爲圖像。通過這種方式,每條路徑都可以連接成一個單一的圖像,這對繪圖來說效率會更高。代碼片段請參閱this question