在向左滑动一个帧超过固定点然后通过向右滑动改变方向以更新帧时获取运动翻译差异
Get Movement Translation Difference on swiping left a frame beyond a fixed point then changing direction by swiping right to update the frame
场景如下:用户点击并按住手指向左滑动 UIView 以显示下方的另一个视图。可见视图可向左滚动 50 点,然后停止。视图的框架转换为 -50
在此期间,用户一直按住点击并最终向右改变方向并可以滚动,直到视图到达其框架平移为 0 的初始位置。
问题是,当用户滚动超出框架停止的固定点 -50 时,仍会重新记录平移,并且当用户将滑动方向更改为向右时,视图移动时会出现差异,我想消除这种差异.翻译的差异似乎是从视图的停止点到用户改变滑动方向的点。这是我的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
drabbleLayer.isUserInteractionEnabled = true
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
drabbleLayer.addGestureRecognizer(recognizer)
}
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
if recognizer.state == .began {
originalCenter = self.drabbleLayer!.center
}
var velocity = recognizer.velocity(in: drabbleLayer)
if recognizer.state == .changed {
let targetX: CGFloat = -50.0
var tranlastion = recognizer.translation(in: self.drabbleLayer!)
var newX: CGFloat {
return originalCenter.x + tranlastion.x
}
if velocity.x < 0 {
let log = tranlastion.x >= targetX
let log2 = drabbleLayer.frame.origin.x >= targetX
if log && log2 {
dabbleLayer!.center.x = newX
} else { drabbleLayer.center.x = -targetX * 2}
} else {
//this is where the Difference in Translation is noticeable if user swipes way beyond the stopping point to the left
if drabbleLayer.frame.origin.x <= 0 {
drabbleLayer!.center.x = newX
} else {
//here view has to get to a full stop. Doesn't seem to work properly.
drabbleLayer.frame.orgin.x = 0}
}
}
}
在我的完整代码中,我打印了翻译值,这样我就可以看到它在哪里发生了变化。我正在检查速度的滑动方向。问题是,在改变方向后,翻译会发生变化,让我们说一个 -74 到 200 的值。在这种情况下,差异将是 74(翻译) - 50(这是停止点,我的代码中的 targetX。此外,这种差异似乎如果用户从右向左滑动,也会出现。
我能否捕捉到翻译的差异并在改变方向之前立即以某种方式消除它?
最后我找到了解决这个问题的办法。我仍然无法找到一个直接的解决方案,比如在改变方向之前保存最新的翻译值,这样我就可以计算翻译的差异。
但是,或者,在改变方向之前,我确实将平移设置为 targetX 值,因此不再需要减去平移中的任何差异。这是我的新代码:
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
var tranlastion = recognizer.translation(in: self.incomeDrabbleLayer!)
var newX: CGFloat {
return originalCenter.x + tranlastion.x
}
let targetX: CGFloat = -50.0
if recognizer.state == .began {
originalCenter = self.incomeDrabbleLayer!.center
}
let velocity = recognizer.velocity(in: incomeDrabbleLayer)
if recognizer.state == .changed {
if velocity.x < 0 {
if incomeDrabbleLayer.frame.origin.x > targetX {
incomeDrabbleLayer.center.x = newX
} else {
incomeDrabbleLayer.frame.origin.x = targetX
let targetTranslationPoint = CGPoint(x: targetX, y: 0)
recognizer.setTranslation(targetTranslationPoint, in: incomeDrabbleLayer)
}
} else {
if incomeDrabbleLayer.frame.origin.x < 0 {
incomeDrabbleLayer.center.x = newX
} else {
incomeDrabbleLayer.frame.origin.x = 0
recognizer.setTranslation(.zero, in: incomeDrabbleLayer)
}
}
}
}
场景如下:用户点击并按住手指向左滑动 UIView 以显示下方的另一个视图。可见视图可向左滚动 50 点,然后停止。视图的框架转换为 -50
在此期间,用户一直按住点击并最终向右改变方向并可以滚动,直到视图到达其框架平移为 0 的初始位置。
问题是,当用户滚动超出框架停止的固定点 -50 时,仍会重新记录平移,并且当用户将滑动方向更改为向右时,视图移动时会出现差异,我想消除这种差异.翻译的差异似乎是从视图的停止点到用户改变滑动方向的点。这是我的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
drabbleLayer.isUserInteractionEnabled = true
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
drabbleLayer.addGestureRecognizer(recognizer)
}
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
if recognizer.state == .began {
originalCenter = self.drabbleLayer!.center
}
var velocity = recognizer.velocity(in: drabbleLayer)
if recognizer.state == .changed {
let targetX: CGFloat = -50.0
var tranlastion = recognizer.translation(in: self.drabbleLayer!)
var newX: CGFloat {
return originalCenter.x + tranlastion.x
}
if velocity.x < 0 {
let log = tranlastion.x >= targetX
let log2 = drabbleLayer.frame.origin.x >= targetX
if log && log2 {
dabbleLayer!.center.x = newX
} else { drabbleLayer.center.x = -targetX * 2}
} else {
//this is where the Difference in Translation is noticeable if user swipes way beyond the stopping point to the left
if drabbleLayer.frame.origin.x <= 0 {
drabbleLayer!.center.x = newX
} else {
//here view has to get to a full stop. Doesn't seem to work properly.
drabbleLayer.frame.orgin.x = 0}
}
}
}
在我的完整代码中,我打印了翻译值,这样我就可以看到它在哪里发生了变化。我正在检查速度的滑动方向。问题是,在改变方向后,翻译会发生变化,让我们说一个 -74 到 200 的值。在这种情况下,差异将是 74(翻译) - 50(这是停止点,我的代码中的 targetX。此外,这种差异似乎如果用户从右向左滑动,也会出现。
我能否捕捉到翻译的差异并在改变方向之前立即以某种方式消除它?
最后我找到了解决这个问题的办法。我仍然无法找到一个直接的解决方案,比如在改变方向之前保存最新的翻译值,这样我就可以计算翻译的差异。
但是,或者,在改变方向之前,我确实将平移设置为 targetX 值,因此不再需要减去平移中的任何差异。这是我的新代码:
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
var tranlastion = recognizer.translation(in: self.incomeDrabbleLayer!)
var newX: CGFloat {
return originalCenter.x + tranlastion.x
}
let targetX: CGFloat = -50.0
if recognizer.state == .began {
originalCenter = self.incomeDrabbleLayer!.center
}
let velocity = recognizer.velocity(in: incomeDrabbleLayer)
if recognizer.state == .changed {
if velocity.x < 0 {
if incomeDrabbleLayer.frame.origin.x > targetX {
incomeDrabbleLayer.center.x = newX
} else {
incomeDrabbleLayer.frame.origin.x = targetX
let targetTranslationPoint = CGPoint(x: targetX, y: 0)
recognizer.setTranslation(targetTranslationPoint, in: incomeDrabbleLayer)
}
} else {
if incomeDrabbleLayer.frame.origin.x < 0 {
incomeDrabbleLayer.center.x = newX
} else {
incomeDrabbleLayer.frame.origin.x = 0
recognizer.setTranslation(.zero, in: incomeDrabbleLayer)
}
}
}
}