How to draw blur shape using UIPanGestureRecognizer — Swift 5

Desired result image
class Rectangle: UIView { var color: UIColor = .clear {  didSet {   backgroundColor = color  } } var borderColor: CGColor = UIColor.lightGray.cgColor {  didSet {   layer.borderColor = borderColor  } } var borderWidth : CGFloat = 0.5 {  didSet {   layer.borderWidth = borderWidth  } }}
override func draw(_ rect: CGRect) {  layer.borderColor = self.borderColor  layer.borderWidth = self.borderWidth}override func didMoveToSuperview() {  addGestureRecognizer(UIPanGestureRecognizer(target: self, action:    #selector(moveRectangleGesture(_:))))}@objc func moveRectangleGesture(_ gesture: UIPanGestureRecognizer) {  frame.origin += gesture.translation(in: self)  gesture.setTranslation(.zero, in: self)}
extension CGPoint { static func +=(lhs: inout CGPoint, rhs: CGPoint) {  lhs.x += rhs.x  lhs.y += rhs.y }}
class EditImageController : UIViewController { lazy var imageToEdit : UIImageView = {  let v = UIImageView(image:  "myImage")  v.frame = CGRect(x: 150, y: 500, width: 200, height: 200)  v.isUserInteractionEnabled = true  v.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(pan(_:))))  return v }()var allRectangles: [Rectangle] = []override func viewDidLoad() { super.viewDidLoad() view.addSubview(imageToEdit)} @objc func pan(_ gesture: UIPanGestureRecognizer) { }}
@objc func pan(_ gesture: UIPanGestureRecognizer) { switch gesture.state {  case .began:   let rectangle = Rectangle(frame: .init(origin:     gesture.location(in: view), size: .init(width: 0, height: 0)))   view.addSubview(rectangle)   allRectangles.append(rectangle)  case .changed:   let distance = gesture.translation(in: view)   let index = allRectangles.index(before: allRectangles.endIndex)   let frame = allRectangles[index].frame   allRectangles[index].frame = .init(origin: frame.origin, size: .init(width: frame.width + distance.x, height: frame.height + distance.y))   allRectangles[index].setNeedsDisplay()   gesture.setTranslation(.zero, in: view)  case .ended:   let index = allRectangles.index(before: allRectangles.endIndex)   addBlur(to: allRectangles[index])   break default:   break }}func addBlur(to View: UIView) { let blurEffect = UIBlurEffect(style: .regular) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = View.bounds blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] View.addSubview(blurEffectView)}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store