Skip to content

Commit

Permalink
Reduced the update frequency of the fps counter. Reduced the size of …
Browse files Browse the repository at this point in the history
…the counter within the RiveView.
  • Loading branch information
duncandoit committed May 11, 2022
1 parent 0bc8f71 commit 4e692a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 2 additions & 4 deletions Source/Components/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ extension RiveView {
advance(delta: 0)
}

showFPS = true
showFPS = false
}

/// Stop playback, clear any created animation or state machine instances.
Expand Down Expand Up @@ -520,10 +520,8 @@ extension RiveView {

// Calculate the time elapsed between ticks
let elapsedTime = timestamp - lastTime
lastTime = timestamp

fpsCounter?.elapsed(time: elapsedTime)

lastTime = timestamp
advance(delta: elapsedTime)
if !isPlaying {
stopTimer()
Expand Down
16 changes: 12 additions & 4 deletions Source/Views/FPSCounterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import UIKit

class FPSCounterView: UILabel {
private let fpsFormatter = NumberFormatter()
private let updateInterval: Double = 0.5
private var timeSinceUpdate: Double = 0

public convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: 75, height: 30))
internal convenience init() {
self.init(frame: CGRect(x: 1, y: 1, width: 70, height: 20))
backgroundColor = .darkGray
textColor = .white
textAlignment = .center
font = UIFont.systemFont(ofSize: 11, weight: .regular)
alpha = 0.75
clipsToBounds = true
layer.cornerRadius = 10
layer.cornerRadius = 5
text = "..."

fpsFormatter.minimumFractionDigits = 2
Expand All @@ -28,7 +31,12 @@ class FPSCounterView: UILabel {

internal func elapsed(time elapsedTime: Double) {
if elapsedTime != 0 {
text = fpsFormatter.string(from: NSNumber(value: 1 / elapsedTime))! + "fps"
timeSinceUpdate += elapsedTime

if timeSinceUpdate >= updateInterval {
timeSinceUpdate = 0
text = fpsFormatter.string(from: NSNumber(value: 1 / elapsedTime))! + "fps"
}
}
}

Expand Down

0 comments on commit 4e692a0

Please sign in to comment.