mirror of
https://github.com/rive-app/rive-ios.git
synced 2026-01-18 17:11:28 +01:00
few bits to sort out - [x] make our mix of simulator/emulator consistent, settling on emulator - [x] passing the factory in works great for just in time asset decoding, but its not amazing when you want to decode ahead of time. - [x] couple of places left to pass this function signature through. (Question) is there a neater way to get this done, feels a bit like we are going back to parameter explosion a bit? - [x] should do a few examples, i think the complexity grows quite a bit in this one as you add caching, or callbacks - [x] should get the cached images/fonts to draw on init as well, either warming up cache, or jitting - [x] examples loading assets from the bundle (also there seem to be actual asset things too? should we use those?!) - [x] add test - [x] re-add "preview" project & rev the preview project once this has been deployed. (do this after new ios deploy) - [x] fix up race condition (see comment) https://github.com/rive-app/rive/assets/1216025/2c14330f-e8a4-481b-bc27-4807cabe3b82 (simple example, both swift ui and standard)  Diffs= fabb7f97f Ios out of band (#6232) Co-authored-by: Gordon Hayes <pggordonhayes@gmail.com> Co-authored-by: Maxwell Talbot <talbot.maxwell@gmail.com>
90 lines
2.6 KiB
Swift
90 lines
2.6 KiB
Swift
//
|
|
// utility.swift
|
|
// RiveExample
|
|
//
|
|
// Created by Maxwell Talbot on 06/05/2021.
|
|
// Copyright © 2021 Rive. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import RiveRuntime
|
|
|
|
@available(*, deprecated, message: "Use method in RiveFile+Extensions instead")
|
|
func getBytes(resourceName: String, resourceExt: String=".riv") -> [UInt8] {
|
|
guard let url = Bundle.main.url(forResource: resourceName, withExtension: resourceExt) else {
|
|
fatalError("Failed to locate \(resourceName) in bundle.")
|
|
}
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
fatalError("Failed to load \(url) from bundle.")
|
|
}
|
|
|
|
// Import the data into a RiveFile
|
|
return [UInt8](data)
|
|
}
|
|
|
|
@available(*, deprecated, message: "Use convenience init in RiveFile+Extensions instead")
|
|
func getRiveFile(resourceName: String, resourceExt: String=".riv", loadCdn: Bool=true) throws -> RiveFile{
|
|
let byteArray = getBytes(resourceName: resourceName, resourceExt: resourceExt)
|
|
return try RiveFile(byteArray: byteArray, loadCdn:loadCdn)
|
|
}
|
|
|
|
struct SwiftVMPlayer: View {
|
|
let viewModels: [RiveViewModel]
|
|
|
|
init(viewModels: RiveViewModel...) {
|
|
self.viewModels = viewModels
|
|
}
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
Color.gray
|
|
|
|
VStack {
|
|
ForEach(0 ..< viewModels.count) { i in
|
|
viewModels[i].view()
|
|
}
|
|
|
|
HStack {
|
|
PlayerButton(title: "play") {
|
|
viewModels.forEach { $0.play() }
|
|
}
|
|
|
|
PlayerButton(title: "pause") {
|
|
viewModels.forEach { $0.pause() }
|
|
}
|
|
|
|
PlayerButton(title: "stop") {
|
|
viewModels.forEach { $0.stop() }
|
|
}
|
|
|
|
PlayerButton(title: "backward.end") {
|
|
viewModels.forEach { $0.reset() }
|
|
}
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
.ignoresSafeArea()
|
|
}
|
|
|
|
struct PlayerButton: View {
|
|
var title: String
|
|
var action: ()->Void
|
|
|
|
var body: some View {
|
|
Button {
|
|
action()
|
|
} label: {
|
|
ZStack {
|
|
Color.blue
|
|
Image(systemName: title + ".fill")
|
|
.foregroundColor(.white)
|
|
}
|
|
.aspectRatio(1, contentMode: .fit)
|
|
.cornerRadius(10)
|
|
.padding()
|
|
}
|
|
}
|
|
}
|
|
}
|