gpt4 book ai didi

ios - 使用 UIPanGestureRecognizer 向下滑动 UIView

转载 作者:行者123 更新时间:2023-11-29 00:05:14 25 4
gpt4 key购买 nike

我的 UIView 中有一个用于 UIPanGestureRecognizerIBAction,我能够处理手势并从一开始就识别状态变化,取消和结束,以及响应这些更改。

但是,当使用 sender.location 处理向下滑动时,UIView 实际上会在手势开始后向上移动,然后继续向下移动。这种体验令人震惊,我不确定自己做错了什么。有人有什么想法吗?

func update(_ translation: CGPoint, origin: CGPoint) {

let offSetY = translation.y

cardView.frame.origin.y = offSetY

let multiplier = 1 - (translation.y / 2000)
self.view.alpha = multiplier
}


func cancel(_ origin: CGPoint) {

let animator = UIViewPropertyAnimator(duration: 0.6, dampingRatio: 0.6) {
self.visualEffectView.alpha = 1
self.cardView.alpha = 1.0
self.view.alpha = 1.0
self.cardView.center = origin
}
animator.startAnimation()
}

func finish() {
let animator = UIViewPropertyAnimator(duration: 0.9, dampingRatio: 0.9) {
self.visualEffectView.effect = nil
self.dismiss(animated: true, completion: nil)
}
animator.startAnimation()
}



@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {

self.view.backgroundColor = .white
let originalCardPosition = cardView.center

//let cardOriginY = cardView.frame.origin.y
let translation = sender.translation(in: self.cardView)
let origin = sender.location(in: self.cardView)

switch sender.state {
case .changed:


if translation.y > 0 { update(translation, origin: origin) }

case .ended:

let translation = sender.translation(in: self.cardView)

if translation.y > 100 {

finish()

} else {
cardView.alpha = 1.0
cancel(originalCardPosition)

}
case .cancelled:
cancel(originalCardPosition)

default: break
}

}

最佳答案

问题在于您将 cardVieworigin.y 直接设置为 translation.y 的值。您需要将 translation.y 添加到手势开始时确定的 View 原始 y 值。

向类添加属性:

var originalY: CGFloat = 0

然后在手势的.began状态下,设置:

originalY = cardView.frame.origin.y

然后在您的 update 方法中设置原点:

cardView.frame.origin.y = originalY + offSetY

关于ios - 使用 UIPanGestureRecognizer 向下滑动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48236474/

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