From d74eb23184b9a206a6105906c590eb788b271350 Mon Sep 17 00:00:00 2001 From: Shin Yamamoto Date: Fri, 9 Apr 2021 18:00:07 +0900 Subject: [PATCH] Fix backdrop alpha's flickers in Maps.app This issue occurs when a panel is swung down with all one's might. The trigger is here. ``` func floatingPanelWillEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer) { if targetState.pointee != .full { owner.searchVC.hideHeader(animated: true) } if targetState.pointee == .tip { >>> vc.contentMode = .static } } ``` However, any library user expect to affect the backdrop by this code. And then I recondiered the reason why the backdrop alpha changes in activateLayout(for:forceLayout:) and it's because the animation using CAAnimation. Therefore I decided to move the point to change the backdrop alpha into the move animation's completion handler. And also the responsibility of `setBackdropAlpha(of:)` was moved into `Core` because `Core` takes on a role of changing the backdrop alpha. --- Sources/Core.swift | 23 ++++++++++++++++++----- Sources/Layout.swift | 10 ---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Sources/Core.swift b/Sources/Core.swift index 4b5c41d4..1274a836 100644 --- a/Sources/Core.swift +++ b/Sources/Core.swift @@ -186,8 +186,19 @@ class Core: NSObject, UIGestureRecognizerDelegate { // MARK: - Layout update - private func updateLayout(to target: FloatingPanelState) { - self.layoutAdapter.activateLayout(for: state, forceLayout: true) + func updateLayout(to target: FloatingPanelState, force: Bool = true) { + self.layoutAdapter.activateLayout(for: target, forceLayout: force) + self.backdropView.alpha = self.getBackdropAlpha(for: target) + } + + private func getBackdropAlpha(for target: FloatingPanelState) -> CGFloat { + let alpha: CGFloat + if target == .hidden { + alpha = 0.0 + } else { + alpha = layoutAdapter.backdropAlpha(for: target) + } + return alpha } func getBackdropAlpha(at cur: CGFloat, with translation: CGFloat) -> CGFloat { @@ -208,11 +219,13 @@ class Core: NSObject, UIGestureRecognizerDelegate { let nextAlpha = layoutAdapter.backdropAlpha(for: nextState) let preAlpha = layoutAdapter.backdropAlpha(for: preState) + let alpha: CGFloat if pre == next { - return preAlpha + alpha = preAlpha } else { - return preAlpha + max(min(1.0, 1.0 - (next - cur) / (next - pre) ), 0.0) * (nextAlpha - preAlpha) + alpha = preAlpha + max(min(1.0, 1.0 - (next - cur) / (next - pre) ), 0.0) * (nextAlpha - preAlpha) } + return alpha } // MARK: - UIGestureRecognizerDelegate @@ -854,7 +867,7 @@ class Core: NSObject, UIGestureRecognizerDelegate { completion: { [weak self] in guard let self = self, self.ownerVC != nil else { return } - self.layoutAdapter.activateLayout(for: targetPosition, forceLayout: true) + self.updateLayout(to: targetPosition) completion() }) moveAnimator?.startAnimation() diff --git a/Sources/Layout.swift b/Sources/Layout.swift index 4a32f067..8e1bac9b 100644 --- a/Sources/Layout.swift +++ b/Sources/Layout.swift @@ -744,8 +744,6 @@ class LayoutAdapter { var state = state - setBackdropAlpha(of: state) - if validStates.contains(state) == false { state = layout.initialState } @@ -775,14 +773,6 @@ class LayoutAdapter { surfaceView.superview?.layoutIfNeeded() } - private func setBackdropAlpha(of target: FloatingPanelState) { - if target == .hidden { - self.backdropView.alpha = 0.0 - } else { - self.backdropView.alpha = backdropAlpha(for: target) - } - } - func backdropAlpha(for state: FloatingPanelState) -> CGFloat { return layout.backdropAlpha?(for: state) ?? defaultLayout.backdropAlpha(for: state) }