下面的代碼通過覆蓋觸摸繪製平滑的曲線,但存在明顯的滯後或延遲。該代碼使用addCurveToPoint
並在每觸摸4個觸點後調用setNeedsDisplay
,這會導致跳動的外觀,因爲圖形跟不上手指的移動。爲了消除滯後或潛在的等待時間,可以使用addQuadCurveToPoint
和addLineToPoint
臨時填充觸點1,2,3(觸及觸點4)。刪除Swift中繪製UIBezierPath平滑線的滯後延遲
如何這實際上代碼來實現刪除使用顯示最終曲線之前的臨時線路和QuadCurved線感知滯後?
如果低於類被附接到一個
UIView
(例如viewOne或self
),如何touchesEnded
後使該圖的一個拷貝到另一個UIView
類(例如viewTwo)以外?// ViewController.swift import UIKit class drawSmoothCurvedLinesWithLagging: UIView { let path=UIBezierPath() var incrementalImage:UIImage? var points = [CGPoint?](count: 5, repeatedValue: nil) var counter:Int? var strokeColor:UIColor? required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func drawRect(rect: CGRect) { autoreleasepool { incrementalImage?.drawInRect(rect) strokeColor = UIColor.blueColor() strokeColor?.setStroke() path.lineWidth = 20 path.lineCapStyle = CGLineCap.Round path.stroke() } } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { counter = 0 let touch: AnyObject? = touches.first points[0] = touch!.locationInView(self) } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch: AnyObject? = touches.first let point = touch!.locationInView(self) counter = counter! + 1 points[counter!] = point if counter == 2{ //use path.addLineToPoint ? //use self.setNeedsDisplay() ? } if counter == 3{ //use path.addQuadCurveToPoint ? //use self.setNeedsDisplay() ? } if counter == 4{ points[3]! = CGPointMake((points[2]!.x + points[4]!.x)/2.0, (points[2]!.y + points[4]!.y)/2.0) path.moveToPoint(points[0]!) path.addCurveToPoint(points[3]!, controlPoint1: points[1]!, controlPoint2: points[2]!) self.setNeedsDisplay() points[0]! = points[3]! points[1]! = points[4]! counter = 1 } } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { self.drawBitmap() self.setNeedsDisplay() path.removeAllPoints() counter = 0 } override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { self.touchesEnded(touches!, withEvent: event) } func drawBitmap(){ UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0.0) strokeColor?.setStroke() if((incrementalImage) == nil){ let rectPath:UIBezierPath = UIBezierPath(rect: self.bounds) UIColor.whiteColor().setFill() rectPath.fill() } incrementalImage?.drawAtPoint(CGPointZero) path.stroke() incrementalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
非常感謝。輝煌!感謝您的明確解釋。我會試着抓住'snapshotImage'。我注意到了代碼中的兩個文物。 1 - 在繪圖,擡起手指,然後再次開始繪製時,看起來前一繪圖的最後部分被移除並用細線代替。這種情況有時會發生。圖片:http://i.imgur.com/LIpfF4q.png 2 - 繪圖時出現一個平直的角落。這也僅在有時發生。 Image:http://i.imgur.com/QyWpXG0.png 什麼是造成這些文物? – user4806509
好的。是的,問題是隻有在調用'drawRect'時才配置屬性,但'constructIncrementalImage'不會。但是,'UIBezierPath'的配置實際上不屬於'drawRect'。當路徑被實例化時,它應該被設置。所以,我寫了一個方法來實例化和配置路徑,並在需要創建路徑時使用它。請參閱上面的修訂答案 – Rob
優秀!非常感謝,它運作良好。 – user4806509