Skip to content

Commit 11b9e2f

Browse files
authored
Merge pull request #54 from rive-app/overloadPlay_50
fixed up examples for isStateMachine delegate flag & added play suppo…
2 parents dabfbf1 + d99c07d commit 11b9e2f

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

Example-iOS/Source/SwiftUI/Button/RiveButtonBridge.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ struct RiveButtonBridge: UIViewRepresentable {
4141
self.rive = rive
4242
}
4343

44-
func play(_ animationName: String) {
44+
func play(_ animationName: String, isStateMachine: Bool) {
4545
rive.play = true
4646
}
4747

48-
func pause(_ animationName: String) {
48+
func pause(_ animationName: String, isStateMachine: Bool) {
4949
rive.play = false
5050
}
5151

52-
func stop(_ animationName: String) {
52+
func stop(_ animationName: String, isStateMachine: Bool) {
5353
rive.play = false
5454
}
5555
}

Example-iOS/Source/SwiftUI/Explorer/RiveExplorerBridge.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ extension RiveExplorerBridge {
181181
loopAction?(animationName, type)
182182
}
183183

184-
func play(_ animationName: String) {
184+
func play(_ animationName: String, isStateMachine: Bool) {
185185
controller.playback = .play
186186
playAction?(animationName)
187187
}
188188

189-
func pause(_ animationName: String) {
189+
func pause(_ animationName: String, isStateMachine: Bool) {
190190
controller.playback = .pause
191191
pauseAction?(animationName)
192192
}

Source/Views/RiveView.swift

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,29 @@ public class RiveView: UIView {
361361
runTimer()
362362
}
363363

364+
/// Plays the list of animations or state machines with optional loop and directions
365+
/// - Parameter animationNames: list of names of the animations to play
366+
/// - Parameter loop: overrides the animation's loop setting
367+
/// - Parameter direction: overrides the animation's default direction (forwards)
368+
/// - Parameter isStateMachine: true of the name refers to a state machine and not an animation
369+
public func play(
370+
animationNames:[String],
371+
loop: Loop = .loopAuto,
372+
direction: Direction = .directionAuto,
373+
isStateMachine: Bool = false
374+
) {
375+
animationNames.forEach{ animationName in
376+
_playAnimation(
377+
animationName:animationName,
378+
loop:loop,
379+
direction:direction,
380+
isStateMachine:isStateMachine
381+
)
382+
}
383+
384+
runTimer()
385+
}
386+
364387

365388
/// Pauses all playing animations and state machines
366389
public func pause() {
@@ -425,6 +448,21 @@ public class RiveView: UIView {
425448
return stateMachineInstances
426449
}
427450

451+
private func _getOrCreateLinearAnimationInstances(
452+
animationName: String
453+
) -> [RiveLinearAnimationInstance]{
454+
let animationInstances = _animations(animationName: animationName)
455+
456+
if (animationInstances.isEmpty){
457+
guard let guardedArtboard=_artboard else {
458+
return []
459+
}
460+
let animationInstance = guardedArtboard.animation(fromName:animationName).instance()
461+
return [animationInstance]
462+
}
463+
return animationInstances
464+
}
465+
428466
private func _playAnimation(
429467
animationName: String,
430468
loop: Loop = .loopAuto,
@@ -437,23 +475,14 @@ public class RiveView: UIView {
437475
_play(stateMachineInstance)
438476
}
439477
} else {
440-
let animationInstances = _animations(animationName: animationName)
478+
let animationInstances = _getOrCreateLinearAnimationInstances(animationName: animationName)
441479

442480
animationInstances.forEach { animationInstance in
443481
_play(
444482
animation:animationInstance,
445483
loop:loop, direction:direction
446484
)
447485
}
448-
if (animationInstances.isEmpty) {
449-
guard let guardedArtboard=_artboard else {
450-
return
451-
}
452-
let animationInstance = guardedArtboard.animation(fromName:animationName).instance()
453-
454-
_play(animation:animationInstance, loop:loop, direction:direction)
455-
456-
}
457486
}
458487
}
459488

0 commit comments

Comments
 (0)