所以我有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
})
}
答案 0 :(得分:2)
我通过添加布尔来检查视图当前是否已展开,然后根据 true或false 来加载相应的动画。我知道这不是最好的做事方式,因为你可以将动画返回原来的位置,而不必编写那么多代码。无论如何,它都有效,所以我会随之滚动。
顺便说一句,我对任何能够帮助完善此代码的人都非常开放,它肯定需要一些TLC。
以下是处理我的panGestureRecogniser
// MARK: - Pan gesture
@objc func panGesture(recognizer: UIPanGestureRecognizer) {
let window = UIApplication.shared.keyWindow
let translation = recognizer.translation(in: self)
let currentY = self.frame.minY
let partialY = (window?.frame.height)!-194
let halfWayPoint = ((window?.frame.height)!/2)-40
self.frame = CGRect(x: 0, y: currentY + translation.y, width: self.frame.width, height: self.frame.height)
recognizer.setTranslation(CGPoint.zero, in: self)
switch recognizer.state {
case .began:
print("Began")
if isExpanded
{
animator = UIViewPropertyAnimator(duration: 0.18, curve: .easeInOut, animations: {
self.requestButton.alpha = 1
self.houseView.alpha = 0
self.officeView.alpha = 0
self.gardenView.alpha = 0
self.carView.alpha = 0
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)
})
self.animator?.isReversed = true
}
else
{
animator = UIViewPropertyAnimator(duration: 0.18, curve: .easeInOut, animations: {
self.requestButton.alpha = 0
self.houseView.alpha = 1
self.officeView.alpha = 1
self.gardenView.alpha = 1
self.carView.alpha = 1
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)
})
self.animator?.isReversed = false
}
animator?.startAnimation()
animator?.pauseAnimation()
case .changed:
// Property animator
let offset: CGFloat = window!.frame.height-194
let fraction: CGFloat = (offset-currentY)/4
let percentage = fraction/100
print(percentage)
animator?.fractionComplete = CGFloat(percentage)
// Prevent scrolling up past y:0
if currentY <= 0
{
self.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
}
case .ended:
// Snap to 80 (y) (expanded)
if currentY < 80 || currentY > 80 && currentY < halfWayPoint
{
self.animator?.stopAnimation(true)
UIView.animate(withDuration:0.62, delay: 0.0, usingSpringWithDamping: 0.62, 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.alpha = 1
self.officeView.alpha = 1
self.gardenView.alpha = 1
self.carView.alpha = 1
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
self.isExpanded = true
})
}
// Snap back to partial (y) (original position)
if currentY > partialY || currentY < partialY && currentY > halfWayPoint
{
self.animator?.stopAnimation(true)
UIView.animate(withDuration:0.62, delay: 0.0, usingSpringWithDamping: 0.62, 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.alpha = 0
self.officeView.alpha = 0
self.gardenView.alpha = 0
self.carView.alpha = 0
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
self.isExpanded = false
})
}
case .cancelled:
print("Cancelled")
case .failed:
print("Failed")
default:
print("Default")
}
}