如何通过拖动其边缘来调整 NSView 的大小?

问题描述

有没有人知道如何通过拖动边缘来系统地调整 NSView 的大小?我会很感激一个工作示例和逻辑解释。提前致谢。

我看到了一个如何为 UIView 执行此操作的示例,因此我使用了这些函数并将其添加到我的 NSView 中,并进行了必要的转换以使其与 NSView 一起使用,但由于某种原因它不起作用。如果有人能指出应该进行的必要调整,我将不胜感激

这是我目前尝试过的,但不起作用

 enum Edge {
            case topLeft,topRight,bottomLeft,bottomright,none
        }

        static var edgeSize: CGFloat = 44.0
        private typealias `Self` = MovableImageView

        var currentEdge: Edge = .none
        var touchStart = CGPoint.zero
        
    override func touchesBegan(with event: NSEvent) {
        if let touch = event.alltouches().first {

        touchStart = touch.location(in: self)

        currentEdge = {
            if self.bounds.size.width - touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
                return .bottomright
            } else if touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
                return .topLeft
            } else if self.bounds.size.width-touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
                return .topRight
            } else if touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
                return .bottomLeft
            }
                return .none
            
                }()
            }
        }
    
    override func touchesMoved(with event: NSEvent) {
        if let touch = event.alltouches().first {
            let currentPoint = touch.location(in: self)
            let prevIoUs = touch.prevIoUsLocation(in: self)

            let originX = self.frame.origin.x
            let originY = self.frame.origin.y
            let width = self.frame.size.width
            let height = self.frame.size.height

            let deltaWidth = currentPoint.x - prevIoUs.x
            let deltaHeight = currentPoint.y - prevIoUs.y

            switch currentEdge {
                case .topLeft:
                    self.frame = CGRect(x: originX + deltaWidth,y: originY + deltaHeight,width: width - deltaWidth,height: height - deltaHeight)
                case .topRight:
                    self.frame = CGRect(x: originX,width: width + deltaWidth,height: height - deltaHeight)
                case .bottomright:
                    self.frame = CGRect(x: originX,y: originY,height: height + deltaWidth)
                case .bottomLeft:
                    self.frame = CGRect(x: originX + deltaWidth,height: height + deltaHeight)
                default:
                    var center = CGPoint(x: (self.frame.origin.x + (self.frame.size.width / 2)),y: (self.frame.origin.y + (self.frame.size.height / 2)))
                    self.center = CGPoint(x: center.x + currentPoint.x - touchStart.x,y: center.y + currentPoint.y - touchStart.y)
                    }
                }
            }
    
    override func touchesEnded(with event: NSEvent) {
        currentEdge = .none
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)