示例工程 38-gestures
38-gestures/Sources/Ch38Kit/Gestures.swift
import SwiftUI
/// 拖拽的纯计算:限制范围、按速度做惯性投影、吸附到网格。
public enum DragMath {
public static func clamped(_ translation: CGSize, limit: CGSize) -> CGSize {
CGSize(
width: min(max(translation.width, -limit.width), limit.width),
height: min(max(translation.height, -limit.height), limit.height)
)
}
/// 松手后按速度外推 0.15 秒,再夹到边界。
public static func projected(_ translation: CGSize, velocity: CGSize, limit: CGSize) -> CGSize {
let projected = CGSize(
width: translation.width + velocity.width * 0.15,
height: translation.height + velocity.height * 0.15
)
return clamped(projected, limit: limit)
}
public static func snapped(_ value: CGSize, grid: CGFloat) -> CGSize {
CGSize(
width: (value.width / grid).rounded() * grid,
height: (value.height / grid).rounded() * grid
)
}
}
/// 手势组合:同时、顺序、互斥。
public enum GestureComposition: String, CaseIterable, Sendable {
case simultaneously
case sequenced
case exclusively
public var summary: String {
switch self {
case .simultaneously: "两个手势同时识别"
case .sequenced: "第一个成功后开始第二个"
case .exclusively: "只识别其中一个"
}
}
}
/// 拖拽卡片:`@GestureState` 在手势结束时自动复位。
public struct DraggableCard: View {
@GestureState private var drag = CGSize.zero
@State private var committed = CGSize.zero
private let limit = CGSize(width: 80, height: 40)
public init() {}
public var body: some View {
let gesture = DragGesture()
.updating($drag) { value, state, _ in
state = DragMath.clamped(value.translation, limit: limit)
}
.onEnded { value in
committed = DragMath.snapped(
DragMath.clamped(value.translation, limit: limit),
grid: 20
)
}
RoundedRectangle(cornerRadius: 12)
.fill(.blue.opacity(0.2))
.overlay(Text("拖我").font(.system(size: 12, weight: .medium)))
.frame(width: 120, height: 70)
.offset(x: drag.width + committed.width, y: drag.height + committed.height)
.gesture(gesture)
.animation(.spring(response: 0.3, dampingFraction: 0.8), value: drag)
}
}
/// 组合手势:放大 + 旋转同时进行。
public struct TransformCard: View {
@State private var magnification = 1.0
@State private var rotation = Angle.zero
@GestureState private var liveMagnification = 1.0
@GestureState private var liveRotation = Angle.zero
public init() {}
public var body: some View {
let magnify = MagnifyGesture()
.updating($liveMagnification) { value, state, _ in
state = value.magnification
}
.onEnded { value in
magnification = min(max(magnification * value.magnification, 0.5), 2)
}
let rotate = RotateGesture()
.updating($liveRotation) { value, state, _ in
state = value.rotation
}
.onEnded { value in
rotation += value.rotation
}
RoundedRectangle(cornerRadius: 12)
.fill(.orange.opacity(0.25))
.overlay(Text("缩放 / 旋转").font(.system(size: 11)))
.frame(width: 140, height: 80)
.scaleEffect(magnification * liveMagnification)
.rotationEffect(rotation + liveRotation)
.gesture(magnify.simultaneously(with: rotate))
}
}
/// 悬停反馈:macOS 的 `onHover` 只影响外观,不参与手势识别。
public struct HoverCard: View {
@State private var hovering = false
public init() {}
public var body: some View {
RoundedRectangle(cornerRadius: 10)
.fill(hovering ? Color.blue.opacity(0.25) : Color.gray.opacity(0.12))
.overlay(Text(hovering ? "悬停中" : "悬停看看").font(.system(size: 11)))
.frame(width: 120, height: 44)
.onHover { hovering = $0 }
.contentShape(Rectangle())
}
}
/// 截图板:拖拽卡片、变换卡片、悬停卡片与组合说明。
public struct GestureBoard: View {
public init() {}
public var body: some View {
VStack(alignment: .leading, spacing: 18) {
Text("手势组合")
.font(.system(size: 12, weight: .semibold))
HStack(spacing: 24) {
DraggableCard()
TransformCard()
}
HStack(spacing: 16) {
HoverCard()
VStack(alignment: .leading, spacing: 4) {
ForEach(GestureComposition.allCases, id: \.self) { item in
Text("\(item.rawValue):\(item.summary)")
.font(.system(size: 10, design: .monospaced))
.foregroundStyle(.secondary)
}
}
}
}
.padding(20)
.frame(width: 420, alignment: .leading)
.background(Color.white)
}
}