Skip to content

Commit 7df15fd

Browse files
authored
ZPosition Transition (#35)
Closes #32.
1 parent c2d61d0 commit 7df15fd

19 files changed

+179
-89
lines changed

Demo/Demo/Swing.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Swing: NavigationTransition {
1414
let angle = 70.0
1515
let offset = 150.0
1616
OnInsertion {
17+
ZPosition(1)
1718
Rotate(.degrees(-angle))
1819
Offset(x: offset)
1920
Opacity()
@@ -24,10 +25,5 @@ struct Swing: NavigationTransition {
2425
Offset(x: -offset)
2526
}
2627
}
27-
OnPop {
28-
OnRemoval {
29-
SendToBack()
30-
}
31-
}
3228
}
3329
}

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct Swing: NavigationTransition {
8686
let angle = 70.0
8787
let offset = 150.0
8888
OnInsertion {
89+
ZPosition(1)
8990
Rotate(.degrees(-angle))
9091
Offset(x: offset)
9192
Opacity()
@@ -96,11 +97,6 @@ struct Swing: NavigationTransition {
9697
Offset(x: -offset)
9798
}
9899
}
99-
OnPop {
100-
OnRemoval {
101-
SendToBack()
102-
}
103-
}
104100
}
105101
}
106102
```

Sources/Animator/AnimatorTransientView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ public class AnimatorTransientView {
3434

3535
@_spi(package)public let uiView: UIView
3636

37-
/// Read-only proxy to underlying `UIView` instance.
37+
/// Read-only proxy to underlying `UIView` properties.
3838
public subscript<T>(dynamicMember keyPath: KeyPath<UIView, T>) -> T {
3939
uiView[keyPath: keyPath]
4040
}
4141

4242
@_spi(package)public init(_ uiView: UIView) {
4343
let properties = Properties(
4444
alpha: uiView.alpha,
45-
transform: uiView.transform
45+
transform: uiView.transform,
46+
layer: .init(
47+
zPosition: uiView.layer.zPosition
48+
)
4649
)
4750
self.initial = properties
4851
self.animation = properties
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import UIKit
2+
3+
/// Defines the allowed mutable properties in a transient view throughout each stage of the transition.
4+
public struct AnimatorTransientViewLayerProperties: Equatable {
5+
/// A proxy for `CALayer.zPosition`.
6+
@OptionalWithDefault
7+
public var zPosition: CGFloat
8+
9+
func assignToUIView(_ uiView: UIView) {
10+
$zPosition.assignTo(uiView, \.layer.zPosition)
11+
}
12+
}

Sources/Animator/AnimatorTransientViewProperties.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ import UIKit
22

33
/// Defines the allowed mutable properties in a transient view throughout each stage of the transition.
44
public struct AnimatorTransientViewProperties: Equatable {
5+
public typealias Layer = AnimatorTransientViewLayerProperties
6+
7+
/// A proxy for `UIView.alpha`.
58
@OptionalWithDefault
69
public var alpha: CGFloat
10+
11+
/// A proxy for `UIView.transform`.
712
@OptionalWithDefault
813
public var transform: CGAffineTransform
914

15+
/// A proxy for `UIView.layer`.
16+
@OptionalWithDefault
17+
public var layer: Layer
18+
1019
func assignToUIView(_ uiView: UIView) {
1120
$alpha.assignTo(uiView, \.alpha)
1221
$transform.assignTo(uiView, \.transform)
13-
}
14-
}
15-
16-
private extension Optional {
17-
func assignTo<Root: AnyObject>(_ root: Root, _ valueKeyPath: ReferenceWritableKeyPath<Root, Wrapped>) {
18-
if let value = self {
19-
root[keyPath: valueKeyPath] = value
20-
}
22+
$layer?.assignToUIView(uiView)
2123
}
2224
}
2325

Sources/Animator/OptionalWithDefault.swift renamed to Sources/Animator/OptionalExtensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ public struct OptionalWithDefault<Value> {
1515
}
1616

1717
extension OptionalWithDefault: Equatable where Value: Equatable {}
18+
19+
extension Optional {
20+
func assignTo<Root: AnyObject>(_ root: Root, _ valueKeyPath: ReferenceWritableKeyPath<Root, Wrapped>) {
21+
if let value = self {
22+
root[keyPath: valueKeyPath] = value
23+
}
24+
}
25+
}

Sources/AtomicTransition/Opacity.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public struct Opacity: MirrorableAtomicTransition {
1515
}
1616
}
1717

18+
@inlinable
1819
public func mirrored() -> Self {
1920
self
2021
}

Sources/AtomicTransition/Scale.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct Scale: MirrorableAtomicTransition {
2828
}
2929
}
3030

31+
@inlinable
3132
public func mirrored() -> Self {
3233
self
3334
}

Sources/AtomicTransition/ZPosition.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
@_spi(package) import Animator
2-
import class UIKit.UIView
2+
import UIKit
3+
4+
/// A transition that changes the view layer’s position on the z axis.
5+
public struct ZPosition: MirrorableAtomicTransition {
6+
private var zPosition: CGFloat
7+
8+
public init(_ zPosition: CGFloat) {
9+
self.zPosition = zPosition
10+
}
11+
12+
public func transition(_ view: TransientView, for operation: TransitionOperation, in container: Container) {
13+
view.animation.layer.zPosition = zPosition
14+
view.completion.layer.zPosition = 0
15+
}
16+
17+
@inlinable
18+
public func mirrored() -> Self {
19+
self
20+
}
21+
}
322

423
/// A transition that brings the view to the front, regardless of insertion or removal.
524
public struct BringToFront: AtomicTransition {

Sources/NavigationTransition/Fade.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,17 @@ public struct Fade: NavigationTransition {
2626
case .in:
2727
MirrorPush {
2828
OnInsertion {
29+
ZPosition(1)
2930
Opacity()
3031
}
3132
}
32-
OnPop {
33-
OnInsertion {
34-
BringToFront()
35-
}
36-
}
3733
case .out:
3834
MirrorPush {
3935
OnRemoval {
36+
ZPosition(1)
4037
Opacity()
4138
}
4239
}
43-
OnPush {
44-
OnRemoval {
45-
BringToFront()
46-
}
47-
}
4840
case .cross:
4941
MirrorPush {
5042
Opacity()

0 commit comments

Comments
 (0)