Skip to content

Commit

Permalink
Merge pull request #54 from rive-app/overloadPlay_50
Browse files Browse the repository at this point in the history
fixed up examples for isStateMachine delegate flag & added play suppo…
  • Loading branch information
mjtalbot authored May 25, 2021
2 parents dabfbf1 + d99c07d commit 11b9e2f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Example-iOS/Source/SwiftUI/Button/RiveButtonBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ struct RiveButtonBridge: UIViewRepresentable {
self.rive = rive
}

func play(_ animationName: String) {
func play(_ animationName: String, isStateMachine: Bool) {
rive.play = true
}

func pause(_ animationName: String) {
func pause(_ animationName: String, isStateMachine: Bool) {
rive.play = false
}

func stop(_ animationName: String) {
func stop(_ animationName: String, isStateMachine: Bool) {
rive.play = false
}
}
Expand Down
4 changes: 2 additions & 2 deletions Example-iOS/Source/SwiftUI/Explorer/RiveExplorerBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ extension RiveExplorerBridge {
loopAction?(animationName, type)
}

func play(_ animationName: String) {
func play(_ animationName: String, isStateMachine: Bool) {
controller.playback = .play
playAction?(animationName)
}

func pause(_ animationName: String) {
func pause(_ animationName: String, isStateMachine: Bool) {
controller.playback = .pause
pauseAction?(animationName)
}
Expand Down
49 changes: 39 additions & 10 deletions Source/Views/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,29 @@ public class RiveView: UIView {
runTimer()
}

/// Plays the list of animations or state machines with optional loop and directions
/// - Parameter animationNames: list of names of the animations to play
/// - Parameter loop: overrides the animation's loop setting
/// - Parameter direction: overrides the animation's default direction (forwards)
/// - Parameter isStateMachine: true of the name refers to a state machine and not an animation
public func play(
animationNames:[String],
loop: Loop = .loopAuto,
direction: Direction = .directionAuto,
isStateMachine: Bool = false
) {
animationNames.forEach{ animationName in
_playAnimation(
animationName:animationName,
loop:loop,
direction:direction,
isStateMachine:isStateMachine
)
}

runTimer()
}


/// Pauses all playing animations and state machines
public func pause() {
Expand Down Expand Up @@ -425,6 +448,21 @@ public class RiveView: UIView {
return stateMachineInstances
}

private func _getOrCreateLinearAnimationInstances(
animationName: String
) -> [RiveLinearAnimationInstance]{
let animationInstances = _animations(animationName: animationName)

if (animationInstances.isEmpty){
guard let guardedArtboard=_artboard else {
return []
}
let animationInstance = guardedArtboard.animation(fromName:animationName).instance()
return [animationInstance]
}
return animationInstances
}

private func _playAnimation(
animationName: String,
loop: Loop = .loopAuto,
Expand All @@ -437,23 +475,14 @@ public class RiveView: UIView {
_play(stateMachineInstance)
}
} else {
let animationInstances = _animations(animationName: animationName)
let animationInstances = _getOrCreateLinearAnimationInstances(animationName: animationName)

animationInstances.forEach { animationInstance in
_play(
animation:animationInstance,
loop:loop, direction:direction
)
}
if (animationInstances.isEmpty) {
guard let guardedArtboard=_artboard else {
return
}
let animationInstance = guardedArtboard.animation(fromName:animationName).instance()

_play(animation:animationInstance, loop:loop, direction:direction)

}
}
}

Expand Down

0 comments on commit 11b9e2f

Please sign in to comment.