gpt4 book ai didi

ios - 在 UIPanGestureRecognizer 更改状态中更改 UIView 的 alpha

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:44 25 4
gpt4 key购买 nike

我有一张卡片 View ,如下图所示。我想在用户刷卡时淡出第一张卡片 UIView) 并淡入第二张卡片。 UIView 可左右切换。我只在下面找到关于这个问题的帖子,但帮不了我。

Change View Alpha on Pan Gesture or Dragging

/// This method handles the swiping gesture on each card.
@objc func handleCardPan(sender: UIPanGestureRecognizer) {

// 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])
print(panLocationInView.x)
print(panLocationInCard)

guard let view = sender.view as? ImageCard else { return }
if(view.layer.zPosition != CGFloat(cards.count)){ return }
switch sender.state {
case .began:
dynamicAnimator.removeAllBehaviors()
// card is attached to center
//cardAttachmentBehavior = UIAttachmentBehavior(item: cards[0], offsetFromCenter: offset, attachedToAnchor: panLocationInView)
let translation = sender.translation(in: self.view)
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)
break
case .changed:
let translation = sender.translation(in: self.view)
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)

// view.alpha = panLocationInView.x / self.view.frame.width
//cards[1].imageView.alpha = 0.0 - alphaPercentage
break
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.difference + cards[0].frame.height / 2))
dynamicAnimator.addBehavior(snapBehavior)
} else {
let velocity = sender.velocity(in: self.view)
let pushBehavior = UIPushBehavior(items: [cards[0]], mode: .instantaneous)
//velocity.y add this for y position vector
pushBehavior.pushDirection = CGVector(dx: velocity.x/10, dy: 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))
let currentAngle: Double = 0.0
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
}
}

screenshot

最佳答案

这是我针对这种情况成功执行的解决方案:

 case .changed:
let translation = sender.translation(in: self.view)
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)

let percentage = (sender.view!.center.x*100.0 / self.view.center.x)/100.0

let alpha = percentage >= 1.0 ? (1.0 - (percentage-1.0)) : percentage

sender.view!.alpha = alpha
cards[1].imageView.alpha = previousCardAlpha + (1.0 - alpha) // previousCardAlpha == cards[1].alpha eg: 0.5

break

此代码在带有 swift 4.2 的 XCode 10 中运行良好。希望这对您有用。

关于ios - 在 UIPanGestureRecognizer 更改状态中更改 UIView 的 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538836/

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