Skip to content

Commit 98decea

Browse files
authored
Release 1.0.1 (#4)
Release `1.0.1`
2 parents 98af23a + f57dc1e commit 98decea

File tree

4 files changed

+184
-148
lines changed

4 files changed

+184
-148
lines changed

Diff for: CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
#### 1.x Releases
5+
- `1.0.x` Releases - [1.0.0](#100) | [1.0.1](#101)
6+
7+
## [1.0.1](https://github.com/space-code/transitions/releases/tag/1.0.1)
8+
Released on 2025-02-10.
9+
10+
#### Added
11+
- Implement `CoreTransition`.
12+
- Added in Pull Request [#93](https://github.com/space-code/transitions/pull/3)
13+
414
## [1.0.0](https://github.com/space-code/transitions/releases/tag/1.0.0)
515
Released on 2025-01-13.
616

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
//
2+
// transitions
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import UIKit
7+
8+
// MARK: - CoreTransition
9+
10+
/// A core transition class.
11+
/// Provides functionality for handling custom view controller transitions.
12+
open class CoreTransition: NSObject, ICoreTransition {
13+
// MARK: Properties
14+
15+
/// The duration of the transition animation, measured in seconds.
16+
public let animationDuration: TimeInterval
17+
18+
/// A flag indicating whether the transition is for presenting (`true`) or dismissing (`false`) a view controller.
19+
public private(set) var isPresenting = false
20+
21+
/// The view controller that is presenting the view controller in the presented parameter.
22+
public private(set) weak var presentingViewController: UIViewController?
23+
/// The view controller object that is about to be presented onscreen.
24+
public private(set) weak var presentedViewController: UIViewController?
25+
26+
/// The context object containing information about the transition.
27+
public private(set) weak var transitionContext: UIViewControllerContextTransitioning?
28+
/// This is the target view controller that will be presented after the transition.
29+
public private(set) weak var toViewController: UIViewController?
30+
/// This is the currently visible view controller before the transition begins.
31+
public private(set) weak var fromViewController: UIViewController?
32+
/// Determine if the transition is interactive.
33+
public private(set) var isInteractive = false
34+
35+
/// The source view controller.
36+
weak var owningController: UIViewController?
37+
38+
/// The container view serves as the parent view that holds both the from-view and to-view during the transition.
39+
public private(set) weak var transitionContainerView: UIView?
40+
41+
/// An object that drives an interactive animation between one view controller and another.
42+
private var interactionController: UIPercentDrivenInteractiveTransition?
43+
44+
// MARK: Initialization
45+
46+
/// Initializes a new instance of the `CustomTransition` class.
47+
///
48+
/// - Parameter animationDuration: The duration of the transition animation, measured in seconds.
49+
public init(animationDuration: TimeInterval) {
50+
self.animationDuration = animationDuration
51+
}
52+
53+
// MARK: ICoreTransition
54+
55+
open func performTransition() {}
56+
57+
open func performDismissTransition() {}
58+
59+
// MARK: Public
60+
61+
open func prepareForTransition(interactive _: Bool) {}
62+
63+
// MARK: Internal
64+
65+
func prepareTransitionParameters() {}
66+
67+
/// Completes the current transition and cleans up the transition state.
68+
///
69+
/// - Parameter completion: An optional closure called with a Boolean value
70+
/// indicating whether the transition was successful (`true`) or cancelled (`false`).
71+
@MainActor
72+
open func completeTransaction(completion: ((Bool) -> Void)? = nil) {
73+
guard let transitionContext else {
74+
completion?(false)
75+
return
76+
}
77+
78+
let isFinished = !transitionContext.transitionWasCancelled
79+
80+
completion?(isFinished)
81+
82+
transitionContext.completeTransition(isFinished)
83+
84+
self.transitionContext = nil
85+
presentedViewController = nil
86+
presentingViewController = nil
87+
}
88+
}
89+
90+
// MARK: IInteractiveTransition
91+
92+
extension CoreTransition: IInteractiveTransition {
93+
public func beginInteractiveDismissalTransition() {
94+
interactionController = UIPercentDrivenInteractiveTransition()
95+
owningController?.dismiss(animated: true)
96+
}
97+
98+
public func updateInteractiveTransitionToProgress(progress: CGFloat) {
99+
interactionController?.update(progress)
100+
}
101+
102+
public func cancelInteractiveTransition() {
103+
interactionController?.completionSpeed = 0.999
104+
interactionController?.cancel()
105+
106+
interactionController = nil
107+
}
108+
109+
public func finishInteractiveTransition() {
110+
interactionController?.finish()
111+
interactionController = nil
112+
}
113+
}
114+
115+
// MARK: UIViewControllerAnimatedTransitioning
116+
117+
extension CoreTransition: UIViewControllerAnimatedTransitioning {
118+
open func transitionDuration(using _: (any UIViewControllerContextTransitioning)?) -> TimeInterval {
119+
animationDuration
120+
}
121+
122+
open func animateTransition(using transitionContext: any UIViewControllerContextTransitioning) {
123+
self.transitionContext = transitionContext
124+
fromViewController = transitionContext.viewController(forKey: .from)
125+
toViewController = transitionContext.viewController(forKey: .to)
126+
isInteractive = transitionContext.isInteractive
127+
transitionContainerView = transitionContext.containerView
128+
129+
prepareTransitionParameters()
130+
131+
prepareForTransition(interactive: isInteractive)
132+
133+
if isPresenting {
134+
performTransition()
135+
} else {
136+
performDismissTransition()
137+
}
138+
}
139+
}
140+
141+
// MARK: UIViewControllerTransitioningDelegate
142+
143+
extension CoreTransition: UIViewControllerTransitioningDelegate {
144+
public func animationController(
145+
forPresented presented: UIViewController,
146+
presenting: UIViewController,
147+
source _: UIViewController
148+
) -> (any UIViewControllerAnimatedTransitioning)? {
149+
presentingViewController = presenting
150+
presentedViewController = presented
151+
isPresenting = true
152+
return self
153+
}
154+
155+
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
156+
presentingViewController = dismissed
157+
presentedViewController = dismissed.presentingViewController
158+
isPresenting = false
159+
return self
160+
}
161+
162+
open func presentationController(
163+
forPresented _: UIViewController,
164+
presenting _: UIViewController?,
165+
source _: UIViewController
166+
) -> UIPresentationController? {
167+
nil
168+
}
169+
}

Diff for: Sources/Transitions/Classes/Core/Extensions/UIViewController+CustomTransition.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ private enum AssociatedKeys {
1717

1818
public extension UIViewController {
1919
/// A custom transition object associated with the view controller.
20-
var customTransition: CustomTransition? {
20+
var customTransition: CoreTransition? {
2121
get {
2222
withUnsafePointer(to: &AssociatedKeys.customTransitionKey) {
23-
objc_getAssociatedObject(self, $0) as? CustomTransition
23+
objc_getAssociatedObject(self, $0) as? CoreTransition
2424
}
2525
}
2626
set {

Diff for: Sources/Transitions/Classes/CustomTransition.swift

+3-146
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,17 @@ import UIKit
99

1010
/// A custom transition class.
1111
/// Provides functionality for handling custom view controller transitions.
12-
open class CustomTransition: NSObject, ICoreTransition {
12+
open class CustomTransition: CoreTransition {
1313
// MARK: Properties
1414

15-
/// The duration of the transition animation, measured in seconds.
16-
public let animationDuration: TimeInterval
17-
18-
/// A flag indicating whether the transition is for presenting (`true`) or dismissing (`false`) a view controller.
19-
public private(set) var isPresenting = false
20-
21-
/// The view controller that is presenting the view controller in the presented parameter.
22-
public private(set) weak var presentingViewController: UIViewController?
23-
/// The view controller object that is about to be presented onscreen.
24-
public private(set) weak var presentedViewController: UIViewController?
25-
26-
/// The context object containing information about the transition.
27-
public private(set) weak var transitionContext: UIViewControllerContextTransitioning?
28-
/// This is the target view controller that will be presented after the transition.
29-
public private(set) weak var toViewController: UIViewController?
30-
/// This is the currently visible view controller before the transition begins.
31-
public private(set) weak var fromViewController: UIViewController?
32-
/// Determine if the transition is interactive.
33-
public private(set) var isInteractive = false
34-
35-
/// The source view controller.
36-
weak var owningController: UIViewController?
37-
38-
/// The container view serves as the parent view that holds both the from-view and to-view during the transition.
39-
public private(set) weak var transitionContainerView: UIView?
40-
41-
/// An object that drives an interactive animation between one view controller and another.
42-
private var interactionController: UIPercentDrivenInteractiveTransition?
43-
4415
/// This represents the view's transformation state before the transition begins.
4516
public private(set) var intitialTransform: CGAffineTransform?
4617
/// This represents the view's transformation state after the transition is completed.
4718
public private(set) var finalTransform: CGAffineTransform?
4819

49-
// MARK: Initialization
50-
51-
/// Initializes a new instance of the `CustomTransition` class.
52-
///
53-
/// - Parameter animationDuration: The duration of the transition animation, measured in seconds.
54-
public init(animationDuration: TimeInterval) {
55-
self.animationDuration = animationDuration
56-
}
57-
58-
// MARK: ICoreTransition
59-
60-
open func performTransition() {}
61-
62-
open func performDismissTransition() {}
63-
64-
// MARK: Public
20+
// MARK: Internal
6521

66-
open func prepareForTransition(interactive _: Bool) {}
67-
68-
/// Completes the current transition and cleans up the transition state.
69-
///
70-
/// - Parameter completion: An optional closure called with a Boolean value
71-
/// indicating whether the transition was successful (`true`) or cancelled (`false`).
72-
@MainActor
73-
open func completeTransaction(completion: ((Bool) -> Void)? = nil) {
74-
guard let transitionContext else {
75-
completion?(false)
76-
return
77-
}
78-
79-
let isFinished = !transitionContext.transitionWasCancelled
80-
81-
completion?(isFinished)
82-
83-
transitionContext.completeTransition(isFinished)
84-
85-
self.transitionContext = nil
86-
presentedViewController = nil
87-
presentingViewController = nil
88-
}
89-
90-
// MARK: Private
91-
92-
private func prepareTransitionParameters() {
22+
override func prepareTransitionParameters() {
9323
guard let toViewController,
9424
let fromViewController,
9525
let transitionContext,
@@ -119,76 +49,3 @@ open class CustomTransition: NSObject, ICoreTransition {
11949
}
12050
}
12151
}
122-
123-
// MARK: IInteractiveTransition
124-
125-
extension CustomTransition: IInteractiveTransition {
126-
public func beginInteractiveDismissalTransition() {
127-
interactionController = UIPercentDrivenInteractiveTransition()
128-
owningController?.dismiss(animated: true)
129-
}
130-
131-
public func updateInteractiveTransitionToProgress(progress: CGFloat) {
132-
interactionController?.update(progress)
133-
}
134-
135-
public func cancelInteractiveTransition() {
136-
interactionController?.completionSpeed = 0.999
137-
interactionController?.cancel()
138-
139-
interactionController = nil
140-
}
141-
142-
public func finishInteractiveTransition() {
143-
interactionController?.finish()
144-
interactionController = nil
145-
}
146-
}
147-
148-
// MARK: UIViewControllerAnimatedTransitioning
149-
150-
extension CustomTransition: UIViewControllerAnimatedTransitioning {
151-
open func transitionDuration(using _: (any UIViewControllerContextTransitioning)?) -> TimeInterval {
152-
animationDuration
153-
}
154-
155-
open func animateTransition(using transitionContext: any UIViewControllerContextTransitioning) {
156-
self.transitionContext = transitionContext
157-
fromViewController = transitionContext.viewController(forKey: .from)
158-
toViewController = transitionContext.viewController(forKey: .to)
159-
isInteractive = transitionContext.isInteractive
160-
transitionContainerView = transitionContext.containerView
161-
162-
prepareTransitionParameters()
163-
164-
prepareForTransition(interactive: isInteractive)
165-
166-
if isPresenting {
167-
performTransition()
168-
} else {
169-
performDismissTransition()
170-
}
171-
}
172-
}
173-
174-
// MARK: UIViewControllerTransitioningDelegate
175-
176-
extension CustomTransition: UIViewControllerTransitioningDelegate {
177-
public func animationController(
178-
forPresented presented: UIViewController,
179-
presenting: UIViewController,
180-
source _: UIViewController
181-
) -> (any UIViewControllerAnimatedTransitioning)? {
182-
presentingViewController = presenting
183-
presentedViewController = presented
184-
isPresenting = true
185-
return self
186-
}
187-
188-
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
189-
presentingViewController = dismissed
190-
presentedViewController = dismissed.presentingViewController
191-
isPresenting = false
192-
return self
193-
}
194-
}

0 commit comments

Comments
 (0)