拖动手势时将矩形限制在屏幕边缘
Limit rectangle to screen edge on drag gesture
我刚刚开始使用 SwiftUI,我希望找到最好的方法来解决在拖动手势期间将此矩形保持在屏幕边界内的问题。现在它离开边缘直到它到达正方形的中间(我想是因为我正在使用 CGPoint)。
我尝试做一些数学运算来限制矩形,它只在左侧成功,但这似乎是一种糟糕的方法,而且不考虑不同的屏幕尺寸。有人可以帮忙吗?
struct ContentView: View {
@State private var pogPosition = CGPoint()
var body: some View {
PogSound()
.position(pogPosition)
.gesture(
DragGesture()
.onChanged { value in
self.pogPosition = value.location
// Poor solve
if(self.pogPosition.x < 36) {
self.pogPosition.x = 36
}
}
.onEnded { value in
print(value.location)
}
)
}
}
这是一个可能的方法演示(对于任何视图,因为视图框架是动态读取的)。
演示并测试 Xcode 12 / iOS 14
struct ViewSizeKey: PreferenceKey {
static var defaultValue = CGSize.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value = nextValue()
}
}
struct ContentView: View {
@State private var pogPosition = CGPoint()
@State private var size = CGSize.zero
var body: some View {
GeometryReader { gp in
PogSound()
.background(GeometryReader {
Color.clear
.preference(key: ViewSizeKey.self, value: [=10=].frame(in: .local).size)
})
.onPreferenceChange(ViewSizeKey.self) {
self.size = [=10=]
}
.position(pogPosition)
.gesture(
DragGesture()
.onChanged { value in
let rect = gp.frame(in: .local)
.inset(by: UIEdgeInsets(top: size.height / 2.0, left: size.width / 2.0, bottom: size.height / 2.0, right: size.width / 2.0))
if rect.contains(value.location) {
self.pogPosition = value.location
}
}
.onEnded { value in
print(value.location)
}
)
.onAppear {
let rect = gp.frame(in: .local)
self.pogPosition = CGPoint(x: rect.midX, y: rect.midY)
}
}.edgesIgnoringSafeArea(.all)
}
}
我刚刚开始使用 SwiftUI,我希望找到最好的方法来解决在拖动手势期间将此矩形保持在屏幕边界内的问题。现在它离开边缘直到它到达正方形的中间(我想是因为我正在使用 CGPoint)。
我尝试做一些数学运算来限制矩形,它只在左侧成功,但这似乎是一种糟糕的方法,而且不考虑不同的屏幕尺寸。有人可以帮忙吗?
struct ContentView: View {
@State private var pogPosition = CGPoint()
var body: some View {
PogSound()
.position(pogPosition)
.gesture(
DragGesture()
.onChanged { value in
self.pogPosition = value.location
// Poor solve
if(self.pogPosition.x < 36) {
self.pogPosition.x = 36
}
}
.onEnded { value in
print(value.location)
}
)
}
}
这是一个可能的方法演示(对于任何视图,因为视图框架是动态读取的)。
演示并测试 Xcode 12 / iOS 14
struct ViewSizeKey: PreferenceKey {
static var defaultValue = CGSize.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value = nextValue()
}
}
struct ContentView: View {
@State private var pogPosition = CGPoint()
@State private var size = CGSize.zero
var body: some View {
GeometryReader { gp in
PogSound()
.background(GeometryReader {
Color.clear
.preference(key: ViewSizeKey.self, value: [=10=].frame(in: .local).size)
})
.onPreferenceChange(ViewSizeKey.self) {
self.size = [=10=]
}
.position(pogPosition)
.gesture(
DragGesture()
.onChanged { value in
let rect = gp.frame(in: .local)
.inset(by: UIEdgeInsets(top: size.height / 2.0, left: size.width / 2.0, bottom: size.height / 2.0, right: size.width / 2.0))
if rect.contains(value.location) {
self.pogPosition = value.location
}
}
.onEnded { value in
print(value.location)
}
)
.onAppear {
let rect = gp.frame(in: .local)
self.pogPosition = CGPoint(x: rect.midX, y: rect.midY)
}
}.edgesIgnoringSafeArea(.all)
}
}