如何解决如何通过拖动其边缘来调整 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 举报,一经查实,本站将立刻删除。