zl程序教程

您现在的位置是:首页 >  其他

当前栏目

绘制采用核芯显卡

2023-04-18 12:52:46 时间

我想让用户绘制一个矩形上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时。

Mohmed Showekh

回答

您正在用以前的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 
    } 

} 

绘制采用核芯显卡