【发布时间】:2018-10-09 22:49:11
【问题描述】:
所以我有一个UIView 附加了一个PanGestureRecognizer,它允许我上下移动视图。当我向上移动视图时,我想降低按钮的 alpha,而当我向下移动视图时,我想将其恢复正常。
我曾尝试在按钮卡入某个位置时单独为其设置动画,但这会破坏我的PropertyAnimator。
我当前的实现工作发现,假设我的手指在视图上,因为这会从识别器返回 .changed 状态。但是我也有实现,当视图在 y 轴 的某个点释放时,将视图捕捉到位。问题是它没有达到 .changed 状态,因此改变了我的hideAnimator.fractionComplete,让按钮在动画中途停留。
初始化属性动画方法
func initPropertyAnimator() {
hideAnimator = UIViewPropertyAnimator(duration: 0.18, curve: .easeInOut, animations: {
self.requestButton.alpha = 0
})
}
识别器状态位于 panGesture(recognizer: UIPanGestureRecognizer)
let currentY = self.frame.minY
switch recognizer.state {
case .began:
print("Began")
case .changed:
// Property animator
let offset: CGFloat = window!.frame.height-194
let percentage: CGFloat = (offset-currentY)/100
print(percentage)
hideAnimator?.fractionComplete = CGFloat(percentage)
case .ended:
// Snap back below 80 (y)
if currentY < 80 || currentY > 80 && currentY < halfWayPoint
{
self.animator?.stopAnimation(true)
UIView.animate(withDuration:0.62, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations:
{
recognizer.view!.frame = CGRect(x: 0, y: 80, width: self.frame.width, height: self.frame.height)
self.requestButton.alpha = 0
self.houseView.frame = CGRect(x: 16, y: 87, width: self.boxWidth, height: 68)
self.officeView.frame = CGRect(x: 16, y: 171, width: self.boxWidth, height: 68)
self.carView.frame = CGRect(x: 16, y: 255, width: self.boxWidth, height: 68)
self.gardenView.frame = CGRect(x: 16, y: 339, width: self.boxWidth, height: 68)
}, completion: { (true) in
})
}
// Snap back above partial (y)
if currentY > partialY || currentY < partialY && currentY > halfWayPoint
{
self.animator?.stopAnimation(true)
UIView.animate(withDuration:0.62, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations:
{
recognizer.view!.frame = CGRect(x: 0, y: partialY, width: self.frame.width, height: self.frame.height)
self.requestButton.alpha = 1
self.houseView.frame = CGRect(x: 16, y: 87+200, width: self.boxWidth, height: 68)
self.officeView.frame = CGRect(x: 16, y: 171+200, width: self.boxWidth, height: 68)
self.carView.frame = CGRect(x: 16, y: 255+200, width: self.boxWidth, height: 68)
self.gardenView.frame = CGRect(x: 16, y: 339+200, width: self.boxWidth, height: 68)
}, completion: { (true) in
})
}
【问题讨论】:
-
我实际上已经实现了一个 UIView.animate 调用,它使用平移手势将视图捕捉到位,具体取决于它在屏幕上的位置。但这不会继续更新 UIView 的 Y 位置。
-
好的,现在您已经完全改变了您声称的代码。我真希望这一次是真的。 :) 所以现在也许我只是很密集,但我不明白你所说的关于该代码做什么或不做什么是错误的......
-
我想我解释得不是很好,我的问题是,如果我将 UIView 滚动到一半,然后放手让它恢复原位,它只有一半动画,因为我让进行到一半,propertyAnimator 停止接收 y 位置的
fractionComplete -
对,好吧,在您的
.ended实现中,您已经停止与属性动画师交谈。如果你想让它在这一点上做更多的事情,你必须告诉它你想让它做什么。我不能告诉你怎么做,因为我不清楚你做什么想要它做什么。但是例如对动画师说“快点回到起点”很容易。事实上,这很容易说是首先使用视图属性动画师的一大好处。 -
但是,您的代码似乎只是说
self.animator?.stopAnimation(true),它实际上只是丢弃了所有内容并走开了。如果那不是您想要做的,请不要这样做。
标签: ios swift uiview uigesturerecognizer uipangesturerecognizer