gpt4 book ai didi

swift - 向左滑动框架超出固定点,然后通过向右滑动更新框架来改变方向,获取运动平移差异

转载 作者:行者123 更新时间:2023-11-30 11:12:26 24 4
gpt4 key购买 nike

场景如下:用户点击并按住手指向左滑动 UIView 以显示下面的另一个 View 。可见 View 可向左滚动 50 点,然后停止。 View 的框架平移 -50

在这段时间里,用户连续按住点击,最后将方向更改为向右,并且可以滚动,直到 View 到达其帧平移为 0 的初始位置。

问题是,当用户滚动超出帧停止的 -50 固定点时,仍然会记录平移,并且当用户将滑动方向更改为右侧时, View 会移动,我想消除这种差异。翻译中的差异似乎是从 View 的停止点到用户更改滑动方向的点。这是我的代码:

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

}
}

}

关于swift - 向左滑动框架超出固定点,然后通过向右滑动更新框架来改变方向,获取运动平移差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52043933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com