2010-04-21 142 views
3

如何在iPhone上用手指滑動/觸摸運動繪製平滑線?我有以下代碼,但它不光滑。例如,當我嘗試做一個循環時,它會讓角落/轉彎。我想我必須使用OpenGL。有任何想法嗎?示例代碼?教程?在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(); 
+1

我結束了使用Open GL和顆粒。檢查這個的好例子是Apple的GLPaint示例代碼。 – Sharief 2010-04-22 21:54:04

回答

5

我結束了使用Open GL和一個粒子。檢查這個的好例子是Apple的GLPaint示例代碼。

+0

它的底層原則是什麼? – 2012-05-29 02:28:22

1

你不需要OpenGL。只要保持起點的參考疏通脈絡中touchesMoved,做線沿線的東西:

CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 

這將使得從起點一條直線到當前點。

+0

這段代碼只會形成一條直線,並且不會成爲一條直線,讓我們假設。 – lugte098 2010-04-21 08:12:25

+0

我建議你仔細觀察一些CGPath函數。例如,對於一個圓圈,您可以使用'CGPathAddArc'。 – shosti 2010-04-21 09:22:01

+4

你錯過了這個問題。在Cocoa-touch中,當人們試圖在屏幕上追蹤一個圓時,可以沿着用戶的觸摸路徑獲得touchMoved事件8或12甚至20個點。所以,當一個連接點時,會出現一個鋸齒狀的8,12或20邊的圖形,而不是一個很好的圓圈。我相信OP正在尋找一種做曲線平滑的方法 - 或許用樣條曲線或類似方法。 – Olie 2010-09-07 23:28:44

3

添加到您的代碼,以理順點

CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound); 

這之間的界線會產生巨大的變化