【问题标题】:Swift how to use property animator with pan gestureSwift如何使用带有平移手势的属性动画师
【发布时间】: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


【解决方案1】:

我通过添加 boolean 来检查当前是否展开视图,然后根据 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")
    }
}

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2018-01-22
    • 2014-07-11
    相关资源
    最近更新 更多