2017-08-27 69 views
1

我想讓用戶繪製一個矩形上UIImageView 我加了兩個變量爲先的最後觸摸位置 我添加了這個功能上的UIImageView矩形: -繪製採用核芯顯卡

func draw(from: CGPoint, to: CGPoint) { 
    UIGraphicsBeginImageContext(imageView.frame.size) 
    context = UIGraphicsGetCurrentContext() 
    context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor) 
    context?.setLineWidth(5.0) 
    let currentRect = CGRect(x: from.x, 
          y: from.y, 
          width: to.x - from.x, 
          height: to.y - from.y) 
    context?.addRect(currentRect) 
    context?.drawPath(using: .stroke) 
    context?.strokePath() 
    imageView.image?.draw(in: self.imageView.frame) 
    imageView.image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 

我添加該方法(touchMoved)它吸引許多矩形

我的方法添加到(touchEnded)它繪製一個,但是當用戶移動觸摸它不會出現

screenshot

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

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     lastTouchLocation = touch.location(in: self.view) 
     draw(from: firstTouchLocation, to: lastTouchLocation) 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     lastTouchLocation = touch.location(in: self.view) 
     draw(from: firstTouchLocation, to: lastTouchLocation) 
    } 
} 

我希望讓用戶擴展矩形時touchMoved並繪製touchEnded時。

回答

1

您正在用以前的image加上一個矩形繪製的新圖像替換您的image。不是從圖像視圖中繪製圖像,而是繪製原始圖像。

或者,可以使矩形的形狀圖層,只是更新形狀圖層的路徑:

class ViewController: UIViewController { 

    @IBOutlet weak var imageView: UIImageView! 

    private let shapeLayer: CAShapeLayer = { 
     let _shapeLayer = CAShapeLayer() 
     _shapeLayer.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor 
     _shapeLayer.strokeColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor 
     _shapeLayer.lineWidth = 3 
     return _shapeLayer 
    }() 

    private var startPoint: CGPoint! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     imageView.layer.addSublayer(shapeLayer) 
    } 

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

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     guard let startPoint = startPoint, let touch = touches.first else { return } 

     let point: CGPoint 

     if let predictedTouch = event?.predictedTouches(for: touch)?.last { 
      point = predictedTouch.location(in: imageView) 
     } else { 
      point = touch.location(in: imageView) 
     } 

     updatePath(from: startPoint, to: point) 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     guard let startPoint = startPoint, let touch = touches.first else { return } 

     let point = touch.location(in: imageView) 

     updatePath(from: startPoint, to: point) 
     imageView.image = imageView.snapshot(afterScreenUpdates: true) 
     shapeLayer.path = nil 
    } 

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
     shapeLayer.path = nil 
    } 

    private func updatePath(from startPoint: CGPoint, to point: CGPoint) { 
     let size = CGSize(width: point.x - startPoint.x, height: point.y - startPoint.y) 
     let rect = CGRect(origin: startPoint, size: size) 
     shapeLayer.path = UIBezierPath(rect: rect).cgPath 
    } 

} 

其中:

extension UIView { 
    func snapshot(afterScreenUpdates: Bool = false) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0) 
     drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates) 
     let image = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

這不僅是簡單的,但更有效,太。

國債收益率:

enter image description here

順便說一句,我可能會建議在touchesMoved使用預測觸動。在一個設備上(而不是在模擬器),可以產生一個稍微更響應的用戶界面。

+0

在哪裏我可以找到關於「預測性觸摸」的信息,您知道我可以在哪裏找到一些教程或somethig? –

+1

@ReinierMelian - 參見WWDC 2015 [iOS上的高級觸摸輸入](https://developer.apple.com/videos/play/wwdc2015/233/)。或者只是谷歌「預測教程」,你會看到很多鏈接。 – Rob

+0

非常感謝。但它繪製了一個矩形,並且當一個新的touchBegan事件開始一個新的矩形並刪除了前一個。我想在觸摸結束後保存圖形並開始新的繪圖。 –