Skip to content

Commit

Permalink
Fix backdrop alpha's flickers in Maps.app
Browse files Browse the repository at this point in the history
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<FloatingPanelState>) {
        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.
  • Loading branch information
scenee committed Apr 12, 2021
1 parent 0f4e377 commit e306b9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
12 changes: 8 additions & 4 deletions Sources/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ class Core: NSObject, UIGestureRecognizerDelegate {
// MARK: - Layout update

private func updateLayout(to target: FloatingPanelState) {
self.layoutAdapter.activateLayout(for: state, forceLayout: true)
self.layoutAdapter.activateLayout(for: target, forceLayout: true)
self.backdropView.alpha = self.getBackdropAlpha(for: target)
}

private func getBackdropAlpha(for target: FloatingPanelState) -> CGFloat {
return target == .hidden ? 0.0 : layoutAdapter.backdropAlpha(for: target)
}

func getBackdropAlpha(at cur: CGFloat, with translation: CGFloat) -> CGFloat {
Expand All @@ -210,9 +215,8 @@ class Core: NSObject, UIGestureRecognizerDelegate {

if pre == next {
return preAlpha
} else {
return preAlpha + max(min(1.0, 1.0 - (next - cur) / (next - pre) ), 0.0) * (nextAlpha - preAlpha)
}
return preAlpha + max(min(1.0, 1.0 - (next - cur) / (next - pre) ), 0.0) * (nextAlpha - preAlpha)
}

// MARK: - UIGestureRecognizerDelegate
Expand Down Expand Up @@ -854,7 +858,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()
Expand Down
10 changes: 0 additions & 10 deletions Sources/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,6 @@ class LayoutAdapter {

var state = state

setBackdropAlpha(of: state)

if validStates.contains(state) == false {
state = layout.initialState
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e306b9a

Please sign in to comment.