gpt4 book ai didi

ios - UIPanGestureRecognizer 有时不会进入最终状态

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

我正在开发一个类似于 Tinder 中的卡片 View 。当卡片X原点大于我声明的值时,它会移出屏幕。否则,它会再次粘在中心。我正在 UIPanGestureRecognizer 函数中执行所有这些操作。我可以在 Change 状态下移动 View 。但是,它有时不会进入最终状态,因此卡片既不会移出屏幕也不会再次粘在中心。它只是停留在一些奇怪的地方。

所以我的问题是卡片应该像下面的屏幕截图一样离开屏幕或粘在中间。

我尝试了下面帖子中的解决方案,但没有任何效果:

UIPanGestureRecognizer not calling End state

UIPanGestureRecognizer does not switch to state "End" or "Cancel" if user panned x and y in negative direction

/// This method handles the swiping gesture on each card and shows the appropriate emoji based on the card's center.
@objc func handleCardPan(sender: UIPanGestureRecognizer) {

// Ensure it's a horizontal drag
let velocity = sender.velocity(in: self.view)
if abs(velocity.y) > abs(velocity.x) {
return
}

// if we're in the process of hiding a card, don't let the user interace with the cards yet
if cardIsHiding { return }
// change this to your discretion - it represents how far the user must pan up or down to change the option
// distance user must pan right or left to trigger an option
let requiredOffsetFromCenter: CGFloat = 80

let panLocationInView = sender.location(in: view)
let panLocationInCard = sender.location(in: cards[0])

switch sender.state {
case .began:
dynamicAnimator.removeAllBehaviors()
let offset = UIOffsetMake(cards[0].bounds.midX, panLocationInCard.y)
// card is attached to center
cardAttachmentBehavior = UIAttachmentBehavior(item: cards[0], offsetFromCenter: offset, attachedToAnchor: panLocationInView)
//dynamicAnimator.addBehavior(cardAttachmentBehavior)
let translation = sender.translation(in: self.view)
print(sender.view!.center.x)
sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y)

sender.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
case .changed:
//cardAttachmentBehavior.anchorPoint = panLocationInView
let translation = sender.translation(in: self.view)
print(sender.view!.center.x)
sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y)
sender.setTranslation(CGPoint(x: 0, y: 0), in: self.view)

case .ended:

dynamicAnimator.removeAllBehaviors()

if !(cards[0].center.x > (self.view.center.x + requiredOffsetFromCenter) || cards[0].center.x < (self.view.center.x - requiredOffsetFromCenter)) {
// snap to center
let snapBehavior = UISnapBehavior(item: cards[0], snapTo: CGPoint(x: self.view.frame.midX, y: self.view.frame.midY + 23))
dynamicAnimator.addBehavior(snapBehavior)
} else {
let velocity = sender.velocity(in: self.view)
let pushBehavior = UIPushBehavior(items: [cards[0]], mode: .instantaneous)
pushBehavior.pushDirection = CGVector(dx: velocity.x/10, dy: velocity.y/10)
pushBehavior.magnitude = 175
dynamicAnimator.addBehavior(pushBehavior)

// spin after throwing
var angular = CGFloat.pi / 2 // angular velocity of spin
let currentAngle: Double = atan2(Double(cards[0].transform.b), Double(cards[0].transform.a))

if currentAngle > 0 {
angular = angular * 1
} else {
angular = angular * -1
}
let itemBehavior = UIDynamicItemBehavior(items: [cards[0]])
itemBehavior.friction = 0.2
itemBehavior.allowsRotation = true
itemBehavior.addAngularVelocity(CGFloat(angular), for: cards[0])
dynamicAnimator.addBehavior(itemBehavior)

showNextCard()
hideFrontCard()

}
default:
break
}
}

image

最佳答案

我正在检查是否水平滑动:

let velocity = sender.velocity(in: self.view)
if abs(velocity.y) > abs(velocity.x) {
return
}

由于某种原因,当我水平滑动时它正在返回。当我评论这段代码时,一切都开始工作了:)

关于ios - UIPanGestureRecognizer 有时不会进入最终状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52439753/

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