一个虚拟触控板 macOS 控件。
我有一个项目需要能够更改二维值 (例如 x:y 或 width:height)。该项目的 UI 空间有限,因此我需要一个控件,其行为类似于触控板,用于调整位置值。
所有属性都支持 Cocoa Bindings。
isEnabled
:控件是否启用 (Bool)minX
:表示的最小 x 值 (CGFloat)
minY
:表示的最小 y 值 (CGFloat)
maxX
:表示的最大 x 值 (CGFloat)
maxY
:表示的最大 y 值 (CGFloat)
deltaX
:拖动 x 比例因子 (CGFloat)。 值越高,跟踪速度越快。
deltaY
:拖动 y 比例因子 (CGFloat)。 值越高,跟踪速度越快。
range
:表示可拖动范围的矩形。
position
:当前位置 (CGPoint)x
:当前的 x 位置 (CGFloat)y
:当前的 y 位置 (CGFloat)该控件可以通过提供的代理 (DSFDragSliderProtocol) 报告更新。
/// A drag was started at a particular point
@objc func dragSlider(_ dragSlide: DSFDragSlider, didStartDragAtPoint: CGPoint)
/// The position changed for the specified drag slider
@objc func dragSlider(_ dragSlide: DSFDragSlider, didChangePosition: CGPoint)
/// The user cancelled an active drag (using the ESC key)
@objc func dragSlider(_ dragSlide: DSFDragSlider, didCancelDragAtPoint: CGPoint)
DSFDragSlider 还有一个 SwiftUI 包装器,名为 DSFDragSliderUI
struct ContentView: View {
// The configuration for the drag slider
var configuration = DragSlider.Configuration(
range: CGRect(x: -100, y: -100, width: 200, height: 200),
deltaX: 1,
deltaY: 1
)
// A dynamically updated position as the user drags the control
@State private var currentPosition = CGPoint(x: 50, y: 50)
@State private var isDisabled = false
var body: some View {
DSFDragSliderUI(
configuration: .constant(configuration),
currentPosition: $position
)
.disabled(self.isDisabled)
}
}
MIT License
Copyright (c) 2020 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.