swift5 - 如何通过拖动边缘来调整 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
}
解决方案
推荐阅读
- symfony - Twig 中“@”符号的含义
- macos - 自制程序不适用于带有 High Sierra 的 Macbook Pro
- html - 如何移动多个向上?
- android - 将暗模式实现为 PDF
- python - 将 DataFrame 拆分为两个 DataFrame 并过滤这两个 DataFrame 以具有相同的维度
- java - Java试图将意图读取为整数字符串
- jdbc - Micronaut-Data JDBC - 用于测试和生产的多种方言
- bluetooth - 蓝牙游戏手柄不创建 /dev/input/event
- javascript - 在 vuetify 中使用 datepicker 创建一个可扩展的应用栏
- angular - 在异步管道重新订阅时重建 Angular 子组件