2017-09-13 72 views
3

我想繪製一條使用核心圖形和swift的直線3但是,當touchesmoved被調用時,它會創建多行而不是單行。使用的代碼如下:在swift 3和核心圖形中畫出一條直線

import UIKit 

    class ViewController: UIViewController { 

     @IBOutlet weak var drawingPlace: UIImageView! 

     var startTouch : CGPoint? 
     var secondTouch : CGPoint? 

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
      let touch = touches.first 
      startTouch = touch?.location(in: drawingPlace) 
     } 

     override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
      for touch in touches{ 
       secondTouch = touch.location(in: drawingPlace) 

       UIGraphicsBeginImageContext(drawingPlace.frame.size) 
       drawingPlace.image?.draw(in: CGRect(x: 0, y: 0, width: drawingPlace.frame.width, height: drawingPlace.frame.height)) 
       let bezier = UIBezierPath() 

       bezier.move(to: startTouch!) 
       bezier.addLine(to: secondTouch!) 
       bezier.close() 

       bezier.lineWidth = 4 
       UIColor.blue.set() 
       bezier.stroke() 

       let img = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
       drawingPlace.image = img 
      } 
     } 
    } 

btw touchesEnd創建一個單行,但只有在用戶結束觸摸後。我希望用戶在觸摸時看到正在繪製的線條。 謝謝。

+0

你重寫'touchesMoved'?如果是,請將該代碼添加到問題中? –

+0

@ReinierMelian你的意思是:let touch = touches.last? – mazen

+0

@mazen你的代碼適用於我,你錯過了你的代碼的其他東西? –

回答

2

你需要保持當前context直到的畫線過程結束後,你需要保持當前的圖像和清晰的context,再次提請保存的圖像,並繪製新的生產線。當結束了觸摸被稱爲清潔當前context並保存當前圖像,後啓動的新畫線過程中再次

這是完整的代碼

import UIKit 

class ViewController2: UIViewController { 

    @IBOutlet weak var drawingPlace: UIImageView! 

    var startTouch : CGPoint? 
    var secondTouch : CGPoint? 
    var currentContext : CGContext? 
    var prevImage : UIImage? 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     startTouch = touch?.location(in: drawingPlace) 
    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for touch in touches{ 
      secondTouch = touch.location(in: drawingPlace) 


      if(self.currentContext == nil) 
      { 
       UIGraphicsBeginImageContext(drawingPlace.frame.size) 
       self.currentContext = UIGraphicsGetCurrentContext() 
      }else{ 
       self.currentContext?.clear(CGRect(x: 0, y: 0, width: drawingPlace.frame.width, height: drawingPlace.frame.height)) 
      } 

      self.prevImage?.draw(in: self.drawingPlace.bounds) 

      let bezier = UIBezierPath() 

      bezier.move(to: startTouch!) 
      bezier.addLine(to: secondTouch!) 
      bezier.close() 


      UIColor.blue.set() 

      self.currentContext?.setLineWidth(4) 
      self.currentContext?.addPath(bezier.cgPath) 
      self.currentContext?.strokePath() 
      let img2 = self.currentContext?.makeImage() 
      drawingPlace.image = UIImage.init(cgImage: img2!) 
     } 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 

     self.currentContext = nil 
     self.prevImage = self.drawingPlace.image 
    } 
} 

結果

enter image description here

+1

男人謝謝你從我所有的心臟中感謝你我在這裏約2再次感謝你:') – mazen

+0

歡迎@mazen我很樂意幫助你,快樂的編碼 –

+0

'if(self.prevImage!= nil){'不需要。如果prevImage爲零,則不會調用繪製方法 –