Skip to content

Fix swipe back cancellation leaving views in inconsistent state #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Sources/Animator/AnimatorTransientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public class AnimatorTransientView {
self.uiView = uiView
}

@_spi(package) public func setUIViewProperties(to properties: KeyPath<AnimatorTransientView, Properties>) {
self[keyPath: properties].assignToUIView(uiView)
@_spi(package) public func setUIViewProperties(
to properties: KeyPath<AnimatorTransientView, Properties>,
force: Bool = false
) {
self[keyPath: properties].assignToUIView(uiView, force: force)
}

@_spi(package) public func resetUIViewProperties() {
Properties.default.assignToUIView(uiView, force: true)
}
}
14 changes: 10 additions & 4 deletions Sources/Animator/AnimatorTransientViewProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public struct AnimatorTransientViewProperties: Equatable {
}

extension AnimatorTransientViewProperties {
static let `default` = Self(
alpha: 1,
transform: .identity,
zPosition: 0
)

init(of uiView: UIView) {
self.init(
alpha: uiView.alpha,
Expand All @@ -24,9 +30,9 @@ extension AnimatorTransientViewProperties {
)
}

func assignToUIView(_ uiView: UIView) {
$alpha.assignTo(uiView, \.alpha)
$transform?.assignToUIView(uiView)
$zPosition.assignTo(uiView, \.layer.zPosition)
func assignToUIView(_ uiView: UIView, force: Bool) {
$alpha.assign(to: uiView, \.alpha, force: force)
$transform.assign(to: uiView, force: force)
$zPosition.assign(to: uiView, \.layer.zPosition, force: force)
}
}
27 changes: 19 additions & 8 deletions Sources/Animator/OptionalWithDefault.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
@propertyWrapper
public struct OptionalWithDefault<Value> {
public private(set) var projectedValue: Value? = nil
public var projectedValue: Self { self }

private var defaultValue: Value
public private(set) var value: Value? = nil
public private(set) var defaultValue: Value

public var wrappedValue: Value {
get { projectedValue ?? defaultValue }
set { projectedValue = newValue }
get { value ?? defaultValue }
set { value = newValue }
}

public init(wrappedValue: Value) {
Expand All @@ -16,10 +17,20 @@ public struct OptionalWithDefault<Value> {

extension OptionalWithDefault: Equatable where Value: Equatable {}

extension Optional {
func assignTo<Root: AnyObject>(_ root: Root, _ valueKeyPath: ReferenceWritableKeyPath<Root, Wrapped>) {
if let value = self {
root[keyPath: valueKeyPath] = value
extension OptionalWithDefault {
func assign<Root: AnyObject>(to root: Root, _ valueKeyPath: ReferenceWritableKeyPath<Root, Value>, force: Bool) {
assign(force: force) { root[keyPath: valueKeyPath] = $0 }
}

func assign(force: Bool, handler: (Value) -> Void) {
if let value = { () -> Value? in
if force {
return wrappedValue
} else {
return value
}
}() {
handler(value)
}
}
}
16 changes: 10 additions & 6 deletions Sources/Animator/Transform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit

@dynamicMemberLookup
public struct Transform: Equatable {
private var transform: CATransform3D
fileprivate var transform: CATransform3D

public subscript<T>(dynamicMember keyPath: WritableKeyPath<CATransform3D, T>) -> T {
get { transform[keyPath: keyPath] }
Expand All @@ -12,12 +12,16 @@ public struct Transform: Equatable {
init(_ transform: CATransform3D) {
self.transform = transform
}
}

func assignToUIView(_ uiView: UIView) {
if let transform = transform.affineTransform {
uiView.transform = transform
} else {
uiView.transform3D = transform
extension OptionalWithDefault where Value == Transform {
func assign(to uiView: UIView, force: Bool) {
self.assign(force: force) {
if let transform = $0.transform.affineTransform {
uiView.transform = transform
} else {
uiView.transform3D = $0.transform
}
}
}
}
Expand Down
64 changes: 42 additions & 22 deletions Sources/NavigationTransitions/NavigationTransitionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,54 @@ final class NavigationTransitionAnimatorProvider: NSObject, UIViewControllerAnim
)
cachedAnimators[ObjectIdentifier(transitionContext)] = animator

let container = transitionContext.containerView
guard
let fromUIView = transitionContext.view(forKey: .from),
let toUIView = transitionContext.view(forKey: .to)
else {
return animator
}

fromUIView.isUserInteractionEnabled = false
toUIView.isUserInteractionEnabled = false

switch transition.handler {
case .transient(let handler):
if let (fromView, toView) = transientViews(for: handler, animator: animator, context: transitionContext) {
fromView.setUIViewProperties(to: \.initial)
animator.addAnimations { fromView.setUIViewProperties(to: \.animation) }
animator.addCompletion { _ in fromView.setUIViewProperties(to: \.completion) }

toView.setUIViewProperties(to: \.initial)
animator.addAnimations { toView.setUIViewProperties(to: \.animation) }
animator.addCompletion { _ in toView.setUIViewProperties(to: \.completion) }
if let (fromView, toView) = transientViews(
for: handler,
animator: animator,
context: (container, fromUIView, toUIView)
) {
for view in [fromView, toView] {
view.setUIViewProperties(to: \.initial)
animator.addAnimations { view.setUIViewProperties(to: \.animation) }
animator.addCompletion { _ in
if transitionContext.transitionWasCancelled {
view.resetUIViewProperties()
} else {
view.setUIViewProperties(to: \.completion)
}
}
}
}
case .primitive(let handler):
handler(animator, operation, transitionContext)
}

animator.addCompletion { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
transitionContext.view(forKey: .from)?.isUserInteractionEnabled = true
transitionContext.view(forKey: .to)?.isUserInteractionEnabled = true

fromUIView.isUserInteractionEnabled = true
toUIView.isUserInteractionEnabled = true

// iOS 16 workaround to nudge views into becoming responsive after transition
if transitionContext.transitionWasCancelled {
fromUIView.removeFromSuperview()
container.addSubview(fromUIView)
} else {
toUIView.removeFromSuperview()
container.addSubview(toUIView)
}
}

return animator
Expand All @@ -116,19 +145,10 @@ final class NavigationTransitionAnimatorProvider: NSObject, UIViewControllerAnim
private func transientViews(
for handler: AnyNavigationTransition.TransientHandler,
animator: Animator,
context: UIViewControllerContextTransitioning
context: (container: UIView, fromUIView: UIView, toUIView: UIView)
) -> (fromView: AnimatorTransientView, toView: AnimatorTransientView)? {
guard
let fromUIView = context.view(forKey: .from),
let toUIView = context.view(forKey: .to)
else {
return nil
}

fromUIView.isUserInteractionEnabled = false
toUIView.isUserInteractionEnabled = false
let (container, fromUIView, toUIView) = context

let container = context.containerView
switch operation {
case .push:
container.insertSubview(toUIView, aboveSubview: fromUIView)
Expand All @@ -141,6 +161,6 @@ final class NavigationTransitionAnimatorProvider: NSObject, UIViewControllerAnim

handler(fromView, toView, operation, container)

return (fromView: fromView, toView: toView)
return (fromView, toView)
}
}
5 changes: 4 additions & 1 deletion Sources/TestUtils/AnimatorTransientView+Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ final class UnimplementedAnimatorTransientView: AnimatorTransientView {
super.init(UIView())
}

override public func setUIViewProperties(to properties: KeyPath<AnimatorTransientView, AnimatorTransientView.Properties>) {
override public func setUIViewProperties(
to properties: KeyPath<AnimatorTransientView, AnimatorTransientView.Properties>,
force: Bool
) {
XCTFail("\(Self.self).\(#function) is unimplemented")
}
}
Expand Down
19 changes: 18 additions & 1 deletion Tests/AnimatorTests/AnimatorTransientViewPropertiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,26 @@ extension AnimatorTransientViewPropertiesTests {
sut.transform = .init(.identity.scaled(5))
sut.zPosition = 15

sut.assignToUIView(view)
sut.assignToUIView(view, force: false)
XCTAssertEqual(view.alpha, 0.5)
XCTAssertEqual(view.transform3D, .identity.scaled(5))
XCTAssertEqual(view.layer.zPosition, 15)
}

func testForceAssignToUIView() {
let view = UIView()
view.transform = .identity.scaledBy(x: 5, y: 5)
view.layer.zPosition = 15
XCTAssertEqual(view.alpha, 1)
XCTAssertEqual(view.transform, .identity.scaledBy(x: 5, y: 5))
XCTAssertEqual(view.layer.zPosition, 15)

var sut = AnimatorTransientView.Properties.default
sut.alpha = 0.5

sut.assignToUIView(view, force: true)
XCTAssertEqual(view.alpha, 0.5)
XCTAssertEqual(view.transform3D, .identity)
XCTAssertEqual(view.layer.zPosition, 0)
}
}