Swift, как использовать аниматор свойств с жестом панорамирования - PullRequest
0 голосов
/ 29 апреля 2018

Итак, у меня есть UIView с прикрепленным PanGestureRecognizer, который позволяет мне перемещать вид вверх и вниз. При перемещении вида вверх, я хочу уменьшить альфа моей кнопки, а когда я переместить вид вниз, я хочу увеличить его до нормального .

Я пытался анимировать кнопку отдельно, когда она защелкивается, но это нарушает мой PropertyAnimator.

Моя текущая реализация работает, find, , при условии, что мой палец находится в представлении , поскольку это возвращает состояние .changed из распознавателя. Но у меня также есть реализация, которая привязывает вид к позиции, когда вид освобождается в определенной точке вдоль оси y . Проблема в том, что он не попадает в измененное состояние и поэтому меняет мой 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

            })
        }

1 Ответ

0 голосов
/ 29 апреля 2018

Я решил проблему, добавив логическое , чтобы проверить, расширяется ли представление в данный момент, и затем в зависимости от 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")
    }
}
...