ios - Swift 如何使用带有平移手势的属性动画器

标签 ios swift uiview uigesturerecognizer uipangesturerecognizer

所以我有一个 UIView 附加了一个 PanGestureRecognizer 允许我上下移动 View 。当我向上移动 View 时,我想降低按钮的 alpha,而当我向下移动 View 时,我想将其恢复正常

我曾尝试在按钮卡入某个位置时单独为按钮设置动画,但这会破坏我的 PropertyAnimator

我当前的实现可以找到,前提是我的手指在 View 上,因为这会从识别器返回 .changed 状态。但我也有实现,当 View 在沿 y 轴 的某个点释放时,将 View 对齐到位。问题是它没有达到 .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

            })
        }

最佳答案

我通过添加一个 bool 值 来检查 View 当前是否已展开来解决这个问题,然后根据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")
    }
}

关于ios - Swift 如何使用带有平移手势的属性动画器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50090381/

相关文章:

ios - 如何使用 kAudioSessionProperty_OverrideCategoryMixWithOthers

ios - 适用于 iOS 的 Google 云端硬盘 SDK : issue with queries

ios - 高级自动完成 Swift

ios - 点击后退按钮时在 View 中显示消息

ios - 如何让可滚动的页面 View 左右滑动?

swift - 重写 func +( [ Double ], [ Double ] )

ios - 带有 TextField 和 Button 的 UIView 内的 UIView 不工作

swift - UIView 边界和框架的属性观察者 react 不同

ios - NSCoding 和恢复 View

iOS:如何将图像修剪到有用的部分(删除透明边框)