'NSPanGestureRecognizer Autolayout Constriants

I am using below code to move a view inside boundaries of superview, it works fine but I want to use auto layout instead. Below are the initial constraints moveable view should have and when I move it using gesture I want to update trailingConstraints & bottomConstraints.

width - super view width / 4 height = 9/16 of width trailingConstraints = 0 bottomConstraints = 0

Is is possible to use NSPanGestureRecognizer using auto layout?

import Cocoa

class ViewController: NSViewController {
    
    var movableView = NSView()
    // MARK: - IBOutlet
    @IBOutlet weak var panView: NSView!
    
    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        panView.wantsLayer = true
        panView.layer?.backgroundColor = NSColor.white.cgColor
        movableView.setFrameSize(NSSize(width: 100, height: 100))
        movableView.setFrameOrigin(NSPoint(x: self.panView.frame.size.width - movableView.frame.size.width, y:  0))
        movableView.wantsLayer = true
        if let myLayer1 = movableView.layer {
            myLayer1.backgroundColor = NSColor.red.cgColor
        }
        panView.addSubview(movableView)

        let panRecognizer1 = NSPanGestureRecognizer.init(target: self, action: #selector(panPictureView(_:)))
        movableView.addGestureRecognizer(panRecognizer1)
    }
    
    // MARK: - Pan gesture
    @objc func panPictureView(_ sender: NSPanGestureRecognizer) {
        
        if let movingObject = sender.view {
            let translation = sender.translation(in: self.panView)
            sender.setTranslation(CGPoint.zero, in: self.panView)
            
            let newX = movingObject.frame.origin.x + translation.x
            let newY = movingObject.frame.origin.y + translation.y
            
            let maxX = panView.frame.width - movingObject.frame.width
            let maxY = panView.frame.height - movingObject.frame.height
            
            if newX <= 0 && newY <= 0 {
                let newPosition = CGPoint(x: 0, y: 0)
                movingObject.setFrameOrigin(newPosition)
            } else if newX <= 0  {
                let newPosition = CGPoint(x: 0, y: newY < maxY ? newY : maxY)
                movingObject.setFrameOrigin(newPosition)
            } else if newY <= 0 {
                let newPosition = CGPoint(x: newX < maxX ? newX : maxX, y: 0)
                movingObject.setFrameOrigin(newPosition)
            } else if newX >= maxX && newY >= maxY {
                let newPosition = CGPoint(x: maxX, y: maxY)
                movingObject.setFrameOrigin(newPosition)
            } else if newX >= maxX  {
                let newPosition = CGPoint(x: maxX, y: newY)
                movingObject.setFrameOrigin(newPosition)
            } else if newY >= maxY  {
                let newPosition = CGPoint(x: newX, y: maxY)
                movingObject.setFrameOrigin(newPosition)
            } else {
                let newPosition = CGPoint(x: newX, y: newY)
                movingObject.setFrameOrigin(newPosition)
            }
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source