Last active
March 12, 2024 14:09
-
-
Save waylybaye/7d7a08cbfca0654f24883178578a3ce8 to your computer and use it in GitHub Desktop.
SwiftUI view render performance Indicator
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
// Created by Baye Wayly on 2020/3/13. | |
// Copyright © 2020 Baye. All rights reserved. | |
import SwiftUI | |
struct Measure<Content: View>: View { | |
@State var cost: TimeInterval = 0 | |
var content: Content | |
init(@ViewBuilder builder: () -> Content) { | |
// discard first time | |
content = builder() | |
let start = Date() | |
let count = 50 | |
for _ in 0..<count { | |
content = builder() | |
} | |
self._cost = State(initialValue: Date().timeIntervalSince(start) / Double(count)) | |
} | |
var body: some View { | |
ZStack(alignment: .topLeading) { | |
self.content | |
Text("\(Int(self.cost * 1000_1000))μs") | |
.font(.system(.caption, design: .monospaced)) | |
.padding(.vertical, 3) | |
.padding(.horizontal, 5) | |
.background(Color.orange) | |
.foregroundColor(.white) | |
.clipShape(RoundedRectangle(cornerRadius: 3)) | |
} | |
} | |
} | |
struct Measure_Previews: PreviewProvider { | |
static var previews: some View { | |
Measure { | |
Text("Hello World") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment