-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "Stress Test" to the example iOS app
Diffs= a7e5cc22f Add a "Stress Test" to the example iOS app (#5986) Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com> Co-authored-by: Zach Plata <plata.zach@gmail.com>
- Loading branch information
1 parent
19dbb8e
commit afbd9e2
Showing
12 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
54d736fec499e15bcd2420e61cd370bf11b9b70c | ||
a7e5cc22f727fc5eb6a56ddd1f7ad85b17db2a59 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
c1e10dc4e600d465a09afcc162b65ad5d7e44ee8 | ||
cfd378a136e7d4a3a4afc8e9be97b13514034a96 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// StressTest.swift | ||
// RiveExample | ||
// | ||
// Created by Chris Dalton on 1/30/23. | ||
// Copyright © 2023 Rive. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import RiveRuntime | ||
import SwiftUI | ||
|
||
// Example to test drawing multiple times within a single view | ||
class StressTestViewController: UIViewController { | ||
var viewModel: RiveViewModel? | ||
var rView: CustomRiveView? | ||
|
||
@objc func onTap(_ sender:UITapGestureRecognizer) { | ||
if let riveView = rView { | ||
riveView.drawRepeat += 3; | ||
self.title = "Stress Test (x\(riveView.drawRepeat))" | ||
} | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
let rModel = try! RiveModel(fileName: "marty", extension: ".riv", in: .main) | ||
rView = CustomRiveView(model: rModel, autoPlay: true) | ||
viewModel = RiveViewModel(rModel, animationName: "Animation2") | ||
viewModel!.fit = RiveFit.contain | ||
viewModel!.setView(rView!) | ||
view.addSubview(rView!) | ||
let f = view.frame | ||
let h = UIApplication.shared.statusBarFrame.height + 40 | ||
rView!.frame = CGRect(x:f.minX, y:f.minY + h, width:f.width, height:f.height - h) | ||
|
||
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.onTap (_:))) | ||
rView!.addGestureRecognizer(gesture) | ||
|
||
rView!.showFPS = true | ||
} | ||
} | ||
|
||
// New RiveView that overrides the drawing logic to re-draw the view multiple times. | ||
class CustomRiveView: RiveView { | ||
private var rModel: RiveModel? | ||
public var drawRepeat: Int32 = 1 | ||
init(model: RiveModel, autoPlay: Bool = true) { | ||
super.init() | ||
rModel = model | ||
} | ||
|
||
required init(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
} | ||
override func drawRive(_ rect: CGRect, size: CGSize) { | ||
// This prevents breaking when loading RiveFile async | ||
guard let artboard = rModel?.artboard else { return } | ||
|
||
let newFrame = CGRect(origin: rect.origin, size: size) | ||
align(with: newFrame, contentRect: artboard.bounds(), alignment: .center, fit: .contain) | ||
|
||
// Find a delta by which to advance our animation each repeated draw, that will loop back | ||
// around to the original starting point once we're done drawing. | ||
var delta = 0.0 | ||
if let animation = rModel?.animation { | ||
delta = Double(animation.effectiveDurationInSeconds()) / Double(drawRepeat) | ||
} | ||
let pad:Float = 100.0 | ||
let r = min(drawRepeat, 8) | ||
let x0:Float = Float(r - 1) * 0.5 * -pad | ||
var x:Float = x0 | ||
var y:Float = -pad * 4 | ||
for i in 1...drawRepeat { | ||
if (i & 0x7) == 0 { | ||
y += pad | ||
x = x0 | ||
} | ||
save() | ||
transform(1, xy:0, yx:0, yy:1, tx:x, ty:y); | ||
draw(with: artboard) | ||
restore() | ||
rModel?.animation?.advance(by: delta) | ||
x += pad | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters