首页 > 解决方案 > 如何通过拖动边缘来调整 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, y: originY + deltaHeight, width: width + deltaWidth, height: height - deltaHeight)
                case .bottomRight:
                    self.frame = CGRect(x: originX, y: originY, width: width + deltaWidth, height: height + deltaWidth)
                case .bottomLeft:
                    self.frame = CGRect(x: originX + deltaWidth, y: originY, width: width - 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
    }

标签: swift5nsview

解决方案


推荐阅读