DSFDragSlider

一个虚拟触控板 macOS 控件。

为什么需要它

我有一个项目需要能够更改二维值 (例如 x:y 或 width:height)。该项目的 UI 空间有限,因此我需要一个控件,其行为类似于触控板,用于调整位置值。

特性

属性

所有属性都支持 Cocoa Bindings。

尺寸

位置

代理

该控件可以通过提供的代理 (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)

SwiftUI

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)
   }
}

截图

drawing

许可证

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.