Skip to content

Commit

Permalink
Modifying some examples with new SwiftUI API for the RiveComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
zplata authored and mjtalbot committed Mar 9, 2022
1 parent bf84c97 commit 4b4dd52
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
10 changes: 1 addition & 9 deletions Example-iOS/Source/SwiftUI/Button/RiveButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ import RiveRuntime

struct RiveButton: View {
let resource:String
let controller: RiveController
public init(
resource: String,
action: (() -> Void)?
) {
self.resource = resource
self.action = action
self.controller = RiveController()
}
let controller: RiveController = RiveController()

var action: (() -> Void)? = nil
var body: some View {
Expand Down
24 changes: 19 additions & 5 deletions Example-iOS/Source/SwiftUI/ProgressBar/RiveProgressBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,39 @@
//

import SwiftUI
import RiveRuntime

//struct RiveProgressBar: View {
//
// let resource: String
//
// @Binding var health: Double
//
// var body: some View {
// VStack {
// RiveProgressBarBridge(health: $health)
// .frame(width: 300, height: 75)
// }
// }
//}
struct RiveProgressBar: View {

let resource: String
var resource: String = "life_bar"
var controller: RiveController;

@Binding var health: Double

var body: some View {
VStack {
RiveProgressBarBridge(health: $health)
RiveViewSwift(resource: resource, autoplay: true, stateMachine: "Life Machine", controller: controller)
.frame(width: 300, height: 75)
}
}
}


struct RiveProgressBar_Previews: PreviewProvider {
static var previews: some View {


RiveProgressBar(resource: "liquid", health: Binding.constant(50.0))
RiveProgressBar(resource: "life_bar", controller: RiveController(), health: Binding.constant(50.0))
}
}
23 changes: 18 additions & 5 deletions Example-iOS/Source/SwiftUI/RiveComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct RiveComponents: View {
/// Tracks the health value coming from the slide for the progress bar
@State var health: Double = 100

@State var sliderController: RiveController = RiveController()

var body: some View {
VStack {
HStack {
Expand All @@ -36,12 +38,23 @@ struct RiveComponents: View {
}
VStack {
Text("RiveProgressBar:")
RiveProgressBar(resource: "liquid", health: $health)
RiveProgressBar(resource: "life_bar", controller: sliderController, health: $health)
}
Slider(
value: $health,
in: 0...100
)
// Slider(
// value: $health,
// in: 0...100
// )
Slider(value: Binding(get: {
self.health
}, set: { (newVal) in
self.health = newVal
print(newVal)
try? self.sliderController.setBooleanState("Life Machine", inputName: "100", value: true)
try? self.sliderController.setBooleanState("Life Machine", inputName: "75", value: newVal < 100)
try? self.sliderController.setBooleanState("Life Machine", inputName: "50", value: newVal <= 66)
try? self.sliderController.setBooleanState("Life Machine", inputName: "25", value: newVal <= 33)
try? self.sliderController.setBooleanState("Life Machine", inputName: "0", value: newVal <= 0)
}), in: 0...100)
.padding()
}
}
Expand Down
40 changes: 32 additions & 8 deletions Example-iOS/Source/SwiftUI/Switch/RiveSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,59 @@
//

import SwiftUI
import RiveRuntime

struct RiveSwitch: View {
@State var switchToOn: Bool = false
@State var switchToOff: Bool = false
@State var on: Bool = false

let resource: String
let controller: RiveController = RiveController()
var onAnimation: String = "On"
var offAnimation: String = "Off"
var startAnimation: String = "StartOff"
var action: ((Bool) -> Void)? = nil

var body: some View {
RiveSwitchBridge(resource: resource, fit: .fitCover, switchToOn: $switchToOn, switchToOff: $switchToOff)
RiveViewSwift(resource: resource, fit: .constant(.fitCover), animation: startAnimation, controller: controller)
.frame(width: 100, height: 50)
.onTapGesture {
switchToOn = false
switchToOff = false
if on {
switchToOff = true
} else {
switchToOn = true
}
on = !on
try? controller.play(onAnimation)
action?(on)
}
}
}

//struct RiveSwitch: View {
// @State var switchToOn: Bool = false
// @State var switchToOff: Bool = false
// @State var on: Bool = false
//
// let resource: String
// var onAnimation: String = "On"
// var offAnimation: String = "Off"
// var startAnimation: String = "StartOff"
// var action: ((Bool) -> Void)? = nil
//
// var body: some View {
// RiveSwitchBridge(resource: resource, fit: .fitCover, switchToOn: $switchToOn, switchToOff: $switchToOff)
// .frame(width: 100, height: 50)
// .onTapGesture {
// switchToOn = false
// switchToOff = false
// if on {
// switchToOff = true
// } else {
// switchToOn = true
// }
// on = !on
// action?(on)
// }
// }
//}

struct RiveSwitch_Previews: PreviewProvider {
static var previews: some View {
RiveSwitch(resource: "switch")
Expand Down

0 comments on commit 4b4dd52

Please sign in to comment.