【问题标题】:how to undo last draw stroke in imageContext如何撤消 imageContext 中的最后一次绘制描边
【发布时间】:2021-03-28 17:28:54
【问题描述】:

我希望我的快速代码撤消最后绘制的笔画。我见过类似 PopLast() 的使用,但将其与图像视图 pic 连接不起作用。要进行绘图,我正在使用 GetImageContext。我知道绘图的选择较少。但在 func undo 中,我想撤消最后绘制的笔画。

   import UIKit
class ViewController: UIViewController {
    var startPoint: CGPoint?
    var statPoint = CGPoint.zero
    var swipe = false
    var pic = UIImageView()
    var clearBtn = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        [pic,clearBtn].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
        }
        pic.backgroundColor = .brown
        clearBtn.backgroundColor = .blue
        pic.frame = CGRect(x: 100, y: 100, width: 250, height: 250)
        clearBtn.frame = CGRect(x: 100, y: 350, width: 50, height: 50)
        clearBtn.addTarget(self, action: #selector(undo), for: .touchDown)
    }
    
    @objc func undo(){
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return}
        swipe = false
        statPoint = touch.location(in: self.pic)
    }
    
    var score = 0
    
    func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint)  {
        UIGraphicsBeginImageContext( pic.frame.size)
        
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        
        pic.image?.draw(in: pic.bounds)
        context.move(to: fromPoint)
        context.addLine(to: toPoint)
        context.setLineCap(.round)
        context.setLineWidth(5)
        
        context.setStrokeColor(UIColor.black.cgColor)
        context.strokePath()
        
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        pic.alpha = 1
        
        UIGraphicsEndImageContext()
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }
        swipe = true
        let currentPoint = touch.location(in: pic)
        drawLine(from: statPoint, to: currentPoint)
        statPoint = currentPoint
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !swipe {
            drawLine(from: statPoint, to: statPoint)
        }
        UIGraphicsBeginImageContext(pic.frame.size)
        pic.image?.draw(in: pic.bounds, blendMode: .normal, alpha: 1)
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
}

【问题讨论】:

    标签: swift imageview undo cgpoint uigraphicscontext


    【解决方案1】:

    创建一个扩展 UIView 的新类,将清晰的背景 UIView 作为画布。在画布中,您可以使用

    override func draw(_ rect: CGRect)
    

    在此处保持新堆栈并推送每个新形状并使用 popLast。 您也可以按照本教程进行操作。 https://www.youtube.com/watch?v=E2NTCmEsdSE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-03
      • 2021-12-26
      • 2016-09-22
      • 1970-01-01
      • 2020-08-26
      • 2011-07-06
      • 2012-08-21
      • 2018-12-14
      相关资源
      最近更新 更多