-1

I want my swift code to undo the last drawn stroke. I have seen something like the use of PopLast() but connecting that with image view pic does not work. To do the drawing I am using GetImageContext. Which i know has less options with drawing. But in the func undo I want to undo the last drawn stroke.

   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()
    }
}

1 Answer 1

0

Create a new class that extends UIView put that clear background UIView as the canvas. In canvas, you can use

override func draw(_ rect: CGRect)

Hold the new stack here and push every new shape and use popLast. You can follow this tutorial also. https://www.youtube.com/watch?v=E2NTCmEsdSE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.