-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from rive-app/fixes_5_10
Hacked in play/pause to SwiftUI
- Loading branch information
Showing
6 changed files
with
300 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,123 @@ | ||
import SwiftUI | ||
import Combine | ||
import RiveRuntime | ||
|
||
typealias LoopAction = ((String, Int) -> Void)? | ||
/// Controller manages the state of the Rive animation | ||
class RiveController: ObservableObject { | ||
@Published var fit: Fit | ||
@Published var alignment: RiveRuntime.Alignment | ||
@Published var playback = Playback.play | ||
|
||
init( | ||
fit: Fit = Fit.Contain, | ||
alignment: RiveRuntime.Alignment = RiveRuntime.Alignment.Center, | ||
autoplay: Bool = false | ||
) { | ||
self.fit = fit | ||
self.alignment = alignment | ||
self.playback = autoplay ? Playback.play : Playback.stop | ||
} | ||
|
||
/// Play animations | ||
func play() { | ||
self.playback = Playback.play | ||
} | ||
|
||
/// Pause all animations and state machines | ||
func pause() { | ||
self.playback = Playback.pause | ||
} | ||
} | ||
|
||
|
||
struct UIRiveView: UIViewRepresentable { | ||
|
||
// MARK: - Properties | ||
|
||
let resource: String | ||
var fit: Fit = Fit.Contain | ||
var alignment: RiveRuntime.Alignment = RiveRuntime.Alignment.Center | ||
@ObservedObject var controller: RiveController | ||
|
||
// Delegate handlers for loop and play events | ||
var loopAction: LoopAction = nil | ||
var playAction: PlaybackAction = nil | ||
var pauseAction: PlaybackAction = nil | ||
|
||
// Constructs the view | ||
// MARK: - UIViewRepresentable | ||
|
||
/// Constructs the view | ||
func makeUIView(context: Context) -> RiveView { | ||
let riveView = RiveView(riveFile: getRiveFile(resourceName: resource)) | ||
riveView.setFit(fit: fit) | ||
riveView.setAlignment(alignment: alignment) | ||
let riveView = RiveView( | ||
riveFile: getRiveFile(resourceName: resource), | ||
fit: controller.fit, | ||
alignment: controller.alignment, | ||
autoplay: controller.playback == Playback.play, | ||
loopDelegate: context.coordinator, | ||
playDelegate: context.coordinator, | ||
pauseDelegate: context.coordinator | ||
) | ||
|
||
// Set the delegates | ||
riveView.loopDelegate = context.coordinator | ||
// Try out target-action? | ||
|
||
return riveView | ||
} | ||
|
||
// Called when a bound variable changes state | ||
func updateUIView(_ uiView: RiveView, context: Context) { | ||
print("updateUI") | ||
uiView.setFit(fit: fit) | ||
uiView.setAlignment(alignment: alignment) | ||
/// Called when the view model changes | ||
func updateUIView(_ uiView: RiveView, context: UIViewRepresentableContext<UIRiveView>) { | ||
uiView.fit = controller.fit | ||
uiView.alignment = controller.alignment | ||
uiView.playback = controller.playback | ||
} | ||
|
||
// Constructs a coordinator for managing updating state | ||
func makeCoordinator() -> Coordinator { | ||
// Coordinator(loopCount: $loopCount) | ||
Coordinator(loopAction: loopAction) | ||
Coordinator(controller: controller, loopAction: loopAction, playAction: playAction, pauseAction: pauseAction) | ||
} | ||
|
||
} | ||
|
||
// Coordinator between RiveView and UIRiveView | ||
class Coordinator: NSObject, LoopDelegate { | ||
private var loopAction: LoopAction | ||
extension UIRiveView { | ||
|
||
init(loopAction: LoopAction) { | ||
self.loopAction = loopAction | ||
} | ||
// MARK: - Coordinator | ||
|
||
func loop(_ animationName: String, type: Int) { | ||
loopAction?(animationName, type) | ||
// Coordinator between RiveView and UIRiveView | ||
class Coordinator: NSObject, LoopDelegate, PlayDelegate, PauseDelegate { | ||
|
||
private var controller: RiveController | ||
private var loopAction: LoopAction | ||
private var playAction: PlaybackAction | ||
private var pauseAction: PlaybackAction | ||
var subscribers: [AnyCancellable] = [] | ||
|
||
init(controller: RiveController, loopAction: LoopAction, playAction: PlaybackAction, pauseAction: PlaybackAction) { | ||
self.loopAction = loopAction | ||
self.playAction = playAction | ||
self.pauseAction = pauseAction | ||
self.controller = controller | ||
|
||
// This stuff is all experimental and may get removed | ||
let fitSubscription = controller.$fit.receive(on: RunLoop.main).sink(receiveValue: fitDidChange) | ||
subscribers.append(fitSubscription) | ||
} | ||
|
||
// @Binding private var loopCount: Int | ||
// | ||
// init(loopCount: Binding<Int>) { | ||
// self._loopCount = loopCount | ||
// } | ||
// | ||
// func loop(_ animationName: String, type: Int) { | ||
// loopCount += 1 | ||
// } | ||
|
||
// Cancel subscribers when Coordinator is deinitialized | ||
deinit { | ||
subscribers.forEach { $0.cancel() } | ||
} | ||
|
||
var fitDidChange: (Fit) -> Void = { fit in | ||
print("Fit changed to \(fit)") | ||
} | ||
|
||
func loop(_ animationName: String, type: Int) { | ||
loopAction?(animationName, type) | ||
} | ||
|
||
func play(_ animationName: String) { | ||
playAction?(animationName) | ||
} | ||
|
||
func pause(_ animationName: String) { | ||
pauseAction?(animationName) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.