2011-01-23 207 views
0

我有下面的代碼,但它不光滑。如果我畫一個圓圈,我會看到尖角。如何在iPhone上用手指滑動/觸摸動作繪製平滑線?

UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+2

只是一個快速的音符......你可能想取代你UIGraphicsGetCurrentContext與UIGraphicsBeginImageContext之後一個電話呼叫,並將結果存儲在一個變量。沒有必要爲每一行調用UIGraphicsGetCurrentContext,效率低下且難以閱讀。 – v01d 2011-01-23 10:11:58

回答

0

您已經看到了一些尖角的,因爲你是畫過許多短抗鋸齒線段,這一切都需要超過1/60秒,所以你最終錯過UI觸摸事件的更新,這會導致走到一條更粗糙的路上。

2D CG繪製沒有加速iOS設備上。

如果你想堅持CG繪畫,試着畫只有最後4個左右的線段,也許關掉抗鋸齒,看看這是平滑。然後在路徑繪製完成後填寫剩下的部分(觸摸)。

0

即使有一個完整的60FPS,你會得到的邊緣,而不是曲線。 CG最好的選擇就是使用貝塞爾路徑。 CG之外,樣條線。

0

你需要做的是不是調用繪圖功能每次移動觸摸屏時,反而讓蓄電池和每一個它的調用時增加它的東西。如果它達到一定的閾值,那麼你做的繪圖代碼。但是您應該在第一次調用該方法時運行它。要找到一個很好的門檻,你必須試用它。

static int accum = 0; 
if ((accum == 0) || (accum == threshold)) { 
UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
accum = 0; 
} 
accum++; 
+0

如果這種方法不夠靈敏,可以根據currentPoint和lastPoint之間的距離動態更改閾值,則閾值應與差值成反比。 – Rich 2011-01-23 22:27:24