关键步骤
- 使用
DragGesture
:
- 可以通过
gesture
视图修饰符将DragGesture
添加到文本视图上。
- 配置
DragGesture
的onChanged
和onEnded
回调,onChanged
用于在拖拽过程中更新文本视图的位置,onEnded
用于判断是否放置成功并进行相应处理。
- 实现相关协议:
- 这里主要涉及自定义逻辑来判断放置成功,例如判断放置区域的边界等,不需要特定的SwiftUI内置协议来专门处理这个简单的拖拽放置逻辑,但在复杂场景下可能会用到
DropDelegate
等相关协议(如果需要处理更复杂的拖放交互,如跨应用拖放等)。
示例代码
import SwiftUI
struct DraggableTextView: View {
@State private var offset = CGSize.zero
@State private var isSuccess = false
let dropZoneSize: CGFloat = 200
let text = "Drag me"
var body: some View {
VStack {
Text(text)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
.offset(x: offset.width, y: offset.height)
.gesture(
DragGesture()
.onChanged { value in
offset = value.translation
}
.onEnded { value in
let endX = value.location.x
let endY = value.location.y
// 简单判断是否放置在特定区域(这里假设一个正方形区域在屏幕中心)
let centerX = UIScreen.main.bounds.midX
let centerY = UIScreen.main.bounds.midY
if endX >= centerX - dropZoneSize / 2 && endX <= centerX + dropZoneSize / 2 &&
endY >= centerY - dropZoneSize / 2 && endY <= centerY + dropZoneSize / 2 {
isSuccess = true
} else {
withAnimation {
offset = .zero
}
}
}
)
if isSuccess {
Text("Drop success!")
.padding()
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(10)
}
Rectangle()
.frame(width: dropZoneSize, height: dropZoneSize)
.foregroundColor(Color.gray.opacity(0.3))
.position(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
}
}
}
struct DraggableTextView_Previews: PreviewProvider {
static var previews: some View {
DraggableTextView()
}
}