Skip to content

Commit

Permalink
Merge pull request #63 from rive-app/viewDeInitialize
Browse files Browse the repository at this point in the history
View de initialize
  • Loading branch information
mjtalbot authored Jun 7, 2021
2 parents 1d2b362 + d823236 commit d61fb38
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 47 deletions.
11 changes: 5 additions & 6 deletions Example-iOS/Source/UIkit/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ class LayoutView: UIView {
var fitButtonAction: ButtonAction?
var alignmentButtonAction: ButtonAction?

@IBOutlet var riveView: RiveView!


@IBOutlet weak var riveView: RiveView!
@IBAction func fitButtonTriggered(_ sender: UIButton) {
fitButtonAction?(sender.currentTitle!)
}

@IBAction func alignmentButtonTriggered(_ sender: UIButton) {
alignmentButtonAction?(sender.currentTitle!)
}
Expand Down Expand Up @@ -58,10 +55,11 @@ class LayoutViewController: UIViewController {
default:
fit = .fitContain
}

layoutView.riveView.fit = fit
}

func setAlignmnet(name:String) {
func setAlignmnet(name:String) {
var alignment: Alignment = .alignmentCenter
switch name {
case "Top Left":
Expand Down Expand Up @@ -94,6 +92,7 @@ class LayoutViewController: UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! LayoutView).riveView.stop()
(view as! LayoutView).fitButtonAction = nil
(view as! LayoutView).alignmentButtonAction = nil
}
}
20 changes: 19 additions & 1 deletion Example-iOS/Source/UIkit/LoopMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,25 @@ class LoopModeController: UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! LoopMode).riveView.stop()
(view as! LoopMode).triggeredResetButton = nil
(view as! LoopMode).triggeredForwardsButton = nil
(view as! LoopMode).triggeredAutoButton = nil
(view as! LoopMode).triggeredBackwardsButton = nil

(view as! LoopMode).triggeredRotatePlayButton = nil
(view as! LoopMode).triggeredRotateOneShotButton = nil
(view as! LoopMode).triggeredRotateLoopButton = nil
(view as! LoopMode).triggeredRotatePingPongButton = nil

(view as! LoopMode).triggeredLoopDownPlayButton = nil
(view as! LoopMode).triggeredLoopDownOneShotButton = nil
(view as! LoopMode).triggeredLoopDownLoopButton = nil
(view as! LoopMode).triggeredLoopDownPingPongButton = nil

(view as! LoopMode).triggeredLtrPlayButton = nil
(view as! LoopMode).triggeredLtrLoopButton = nil
(view as! LoopMode).triggeredLtrOneShotButton = nil
(view as! LoopMode).triggeredLtrPingPongButton = nil
}
}

5 changes: 1 addition & 4 deletions Example-iOS/Source/UIkit/MultipleAnimations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MultipleAnimations: UIView {
}

class MultipleAnimationsController: UIViewController {

let loopResourceName = "artboard_animations"

override public func loadView() {
Expand Down Expand Up @@ -48,10 +49,6 @@ class MultipleAnimationsController: UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! MultipleAnimations).squareGoAround.stop()
(view as! MultipleAnimations).squareRollAround.stop()
(view as! MultipleAnimations).circle.stop()
(view as! MultipleAnimations).star.stop()

}
}
1 change: 0 additions & 1 deletion Example-iOS/Source/UIkit/SimpleAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SimpleAnimationViewController: UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! RiveView).stop()
}
}

Expand Down
4 changes: 3 additions & 1 deletion Example-iOS/Source/UIkit/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class StateMachineViewController: UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! StateMachineView).riveView.stop()
(view as! StateMachineView).beginnerButtonAction = nil
(view as! StateMachineView).intermediateButtonAction = nil
(view as! StateMachineView).expertButtonAction = nil
}
}
25 changes: 12 additions & 13 deletions Example-iOS/Source/UIkit/iosPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IOSPlayerView: UIView {
class FileChoiceDelegate: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let choices = ["artboard_animations", "basketball", "clipping", "explorer", "f22", "flux_capacitor", "loopy", "mascot", "neostream", "off_road_car_blog", "progress", "pull", "rope", "skills", "trailblaze", "ui_swipe_left_to_delete", "vader", "wacky", "juice_v7", "truck_v7"]
var chosen = "skills"
var viewController:IOSPlayerViewController?
weak var viewController:IOSPlayerViewController?
//MARK: - Pickerview method
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
Expand All @@ -38,7 +38,7 @@ class FileChoiceDelegate: NSObject, UIPickerViewDataSource, UIPickerViewDelegate
class ArtboardChoicesDelegate: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var choices = [""]
var chosen:String?
var viewController:IOSPlayerViewController?
weak var viewController:IOSPlayerViewController?

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
Expand Down Expand Up @@ -118,20 +118,20 @@ class IOSPlayerViewController: UIViewController {
let play = UIButton(
type: .system,
primaryAction:
UIAction(title: ">", handler: { _ in
UIAction(title: ">", handler: { [unowned self] _ in
self.playerView?.riveView?.play(animationName: name)
}))
let pause = UIButton(
type: .system,
primaryAction:
UIAction(title: "||", handler: { _ in
UIAction(title: "||", handler: { [unowned self] _ in
self.playerView?.riveView?.pause(animationName: name)
}))

let stop = UIButton(
type: .system,
primaryAction:
UIAction(title: "[]", handler: { _ in
UIAction(title: "[]", handler: { [unowned self] _ in
self.playerView?.riveView?.stop(animationName: name)
}))

Expand Down Expand Up @@ -193,20 +193,20 @@ class IOSPlayerViewController: UIViewController {
let play = UIButton(
type: .system,
primaryAction:
UIAction(title: ">", handler: { _ in
UIAction(title: ">", handler: { [unowned self] _ in
self.playerView?.riveView?.play(animationName: name, isStateMachine: true)
}))
let pause = UIButton(
type: .system,
primaryAction:
UIAction(title: "||", handler: { _ in
UIAction(title: "||", handler: { [unowned self] _ in
self.playerView?.riveView?.pause(animationName: name, isStateMachine: true)
}))

let stop = UIButton(
type: .system,
primaryAction:
UIAction(title: "[]", handler: { _ in
UIAction(title: "[]", handler: { [unowned self] _ in
self.playerView?.riveView?.stop(animationName: name, isStateMachine: true)
}))

Expand Down Expand Up @@ -249,7 +249,7 @@ class IOSPlayerViewController: UIViewController {
let switchToggle = UISwitch(
frame: CGRect(),
primaryAction: UIAction(
handler: { this in
handler: { [unowned self] this in
if ((this.sender as! UISwitch).isOn){
self.playerView?.riveView.setBooleanState(name, inputName: inputName, value: true)
}
Expand All @@ -265,7 +265,7 @@ class IOSPlayerViewController: UIViewController {
let fireButton = UIButton(
type: .system,
primaryAction:
UIAction(title: "fire", handler: { _ in
UIAction(title: "fire", handler: { [unowned self] _ in
self.playerView?.riveView.fireState(name, inputName: inputName)
}))
stackView.addArrangedSubview(fireButton)
Expand All @@ -279,7 +279,7 @@ class IOSPlayerViewController: UIViewController {
let downButton = UIButton(
type: .system,
primaryAction:
UIAction(title: "-", handler: { _ in
UIAction(title: "-", handler: { [unowned self] _ in
let currentValue = (valueLabel.text! as NSString)
let currentFloat = currentValue.floatValue - 1
valueLabel.text = NSString(format: "%.2f", currentFloat) as String
Expand All @@ -289,7 +289,7 @@ class IOSPlayerViewController: UIViewController {
let upButton = UIButton(
type: .system,
primaryAction:
UIAction(title: "+", handler: { _ in
UIAction(title: "+", handler: { [unowned self] _ in
let currentValue = (valueLabel.text! as NSString)
let currentFloat = currentValue.floatValue + 1
valueLabel.text = NSString(format: "%.2f", currentFloat) as String
Expand Down Expand Up @@ -334,6 +334,5 @@ class IOSPlayerViewController: UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! IOSPlayerView).riveView.stop()
}
}
4 changes: 2 additions & 2 deletions Source/Renderer/RiveArtboard.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ - (RiveLinearAnimation *)firstAnimation {

- (RiveLinearAnimation *)animationFromIndex:(NSInteger)index {
if (index < 0 || index >= [self animationCount]) {
@throw [[RiveException alloc] initWithName:@"NoAnimationFound" reason:[NSString stringWithFormat: @"No Animation found at index %ld.", index] userInfo:nil];
@throw [[RiveException alloc] initWithName:@"NoAnimationFound" reason:[NSString stringWithFormat: @"No Animation found at index %ld.", (long)index] userInfo:nil];
}
return [[RiveLinearAnimation alloc] initWithAnimation: _artboard->animation(index)];
}
Expand Down Expand Up @@ -82,7 +82,7 @@ - (RiveStateMachine *)firstStateMachine {
// Returns a state machine at the given index, or null if the index is invalid
- (RiveStateMachine *)stateMachineFromIndex:(NSInteger)index {
if (index < 0 || index >= [self stateMachineCount]) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineFound" reason:[NSString stringWithFormat: @"No State Machine found at index %ld.", index] userInfo:nil];
@throw [[RiveException alloc] initWithName:@"NoStateMachineFound" reason:[NSString stringWithFormat: @"No State Machine found at index %ld.", (long)index] userInfo:nil];
}
return [[RiveStateMachine alloc] initWithStateMachine: _artboard->stateMachine(index)];
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Renderer/RiveFile.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ - (nullable instancetype)initWithResource:(NSString *)resourceName withExtension
NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
UInt8 *bytePtr = (UInt8 *)[fileData bytes];

return [[RiveFile alloc] initWithBytes:bytePtr byteLength:fileData.length];
return [self initWithBytes:bytePtr byteLength:fileData.length];
}

/*
* Creates a RiveFile from a binary resource, and assumes the resource extension is '.riv'
*/
- (nullable instancetype)initWithResource:(NSString *)resourceName {
return [[RiveFile alloc] initWithResource:resourceName withExtension:@"riv"];
return [self initWithResource:resourceName withExtension:@"riv"];
}

/*
Expand Down Expand Up @@ -156,7 +156,7 @@ - (NSInteger)artboardCount {

- (RiveArtboard *)artboardFromIndex:(NSInteger)index {
if (index >= [self artboardCount]) {
@throw [[RiveException alloc] initWithName:@"NoArtboardFound" reason:[NSString stringWithFormat: @"No Artboard Found at index %ld.", index] userInfo:nil];
@throw [[RiveException alloc] initWithName:@"NoArtboardFound" reason:[NSString stringWithFormat: @"No Artboard Found at index %ld.", (long)index] userInfo:nil];
}
return [[RiveArtboard alloc]
initWithArtboard: reinterpret_cast<rive::Artboard *>(riveFile->artboard(index))];
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/RiveStateMachine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ - (RiveStateMachineInput *)_convertInput:(const rive::StateMachineInput *)input{
// Creates a new instance of this state machine
- (RiveStateMachineInput *)inputFromIndex:(NSInteger)index {
if (index >= [self inputCount]) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineInputFound" reason:[NSString stringWithFormat: @"No Input found at index %ld.", index] userInfo:nil];
@throw [[RiveException alloc] initWithName:@"NoStateMachineInputFound" reason:[NSString stringWithFormat: @"No Input found at index %ld.", (long)index] userInfo:nil];
}
return [self _convertInput: stateMachine->input(index) ];
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Renderer/RiveStateMachineInstance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ - (RiveSMIInput *)_convertInput:(const rive::SMIInput *)input{
// Creates a new instance of this state machine
- (RiveSMIInput *)inputFromIndex:(NSInteger)index {
if (index >= [self inputCount]) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineInputFound" reason:[NSString stringWithFormat: @"No Input found at index %ld.", index] userInfo:nil];
@throw [[RiveException alloc] initWithName:@"NoStateMachineInputFound" reason:[NSString stringWithFormat: @"No Input found at index %ld.", (long)index] userInfo:nil];
}
return [self _convertInput: instance->input(index) ];
}
Expand Down Expand Up @@ -184,7 +184,7 @@ - (RiveLayerState *)_convertLayerState:(const rive::LayerState *)layerState{
- (RiveLayerState *)stateChangedFromIndex:(NSInteger)index{
const rive::LayerState *layerState = instance->stateChangedByIndex(index);
if (layerState == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoStateChangeFound" reason:[NSString stringWithFormat: @"No State Changed found at index %lu.", index] userInfo:nil];
@throw [[RiveException alloc] initWithName:@"NoStateChangeFound" reason:[NSString stringWithFormat: @"No State Changed found at index %ld.", (long)index] userInfo:nil];
} else {
return [self _convertLayerState: layerState];
}
Expand Down
55 changes: 43 additions & 12 deletions Source/Views/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ struct ConfigOptions {
var autoPlay: Bool = true
}


class CADisplayLinkProxy {

var displayLink: CADisplayLink?
var handle: (() -> Void)?
private var runloop: RunLoop
private var mode: RunLoop.Mode

init(handle: (() -> Void)?, to runloop: RunLoop, forMode mode: RunLoop.Mode) {
self.handle = handle
self.runloop = runloop
self.mode = mode
displayLink = CADisplayLink(target: self, selector: #selector(updateHandle))
displayLink?.add(to: runloop, forMode: mode)
}

@objc func updateHandle() {
handle?()
}

func invalidate() {
displayLink?.remove(from: runloop, forMode: mode)
displayLink?.invalidate()
displayLink = nil
}
}

public class RiveView: UIView {

deinit { print("RiveView is being de initialized") }
Expand All @@ -110,7 +137,7 @@ public class RiveView: UIView {
public var stateMachines: [RiveStateMachineInstance] = []
public var playingStateMachines: Set<RiveStateMachineInstance> = []
private var lastTime: CFTimeInterval = 0
private var displayLink: CADisplayLink?
private var displayLinkProxy: CADisplayLinkProxy?

// Delegates
public weak var loopDelegate: LoopDelegate?
Expand Down Expand Up @@ -228,6 +255,7 @@ extension RiveView {
andStateMachine stateMachine: String?=nil,
andAutoPlay autoPlay: Bool=true
) {
clear()
if !riveFile.isLoaded {
// Save the config details for async call
self.configOptions = ConfigOptions(
Expand All @@ -240,7 +268,7 @@ extension RiveView {
return;
}

clear()

// Testing stuff
NotificationCenter.default.addObserver(self, selector: #selector(animationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification, object: nil)
Expand Down Expand Up @@ -288,6 +316,7 @@ extension RiveView {

/// Stop playback, clear any created animation or state machine instances.
private func clear() {
stop()
playingAnimations.removeAll()
playingStateMachines.removeAll()
animations.removeAll()
Expand Down Expand Up @@ -381,31 +410,33 @@ extension RiveView {
artboard.draw(renderer)
}


// Starts the animation timer
private func runTimer() {
if displayLink == nil {
displayLink = CADisplayLink(target: self, selector: #selector(tick));
// Note: common didnt pause on scroll.
displayLink?.add(to: .main, forMode: .common)
if displayLinkProxy == nil {
displayLinkProxy = CADisplayLinkProxy(
handle: { [weak self] in
self?.tick()
}, to: .main, forMode: .common)
}
if displayLink?.isPaused == true {
lastTime = 0
displayLink!.isPaused = false
if displayLinkProxy?.displayLink?.isPaused == true {
displayLinkProxy?.displayLink?.isPaused = false
}
}

// Stops the animation timer
private func stopTimer() {
displayLink?.invalidate()
displayLink = nil;
displayLinkProxy?.invalidate()
displayLinkProxy = nil
lastTime = 0
}

/// Start a redraw:
/// - determine the elapsed time
/// - advance the artbaord, which will invalidate the display.
/// - if the artboard has come to a stop, stop.
@objc func tick() {
guard let displayLink = displayLink else {
guard let displayLink = displayLinkProxy?.displayLink else {
// Something's gone wrong, clean up and bug out
stopTimer()
return
Expand Down
2 changes: 1 addition & 1 deletion submodules/rive-cpp

0 comments on commit d61fb38

Please sign in to comment.