Created
February 10, 2023 00:59
-
-
Save ryohey/eaedcb18b990bea88aec1c21a4afbd9d to your computer and use it in GitHub Desktop.
SwiftUI redraw test
This file contains hidden or 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
import SwiftUI | |
struct ContentView: View { | |
@State private var count = 0 | |
@State private var states: [InfoView.State] = [.init(text: "", text2: "", text3: "")] | |
var body: some View { | |
VStack { | |
RedrawBorder { | |
Button { | |
count += 1 | |
if count > 10 { | |
let i = Int.random(in: 0..<states.count) | |
states[i].text = "\(count)" | |
states[i].text2 = "updated" | |
} else { | |
states.append(.init(text: "\(count)", text2: "foo", text3: "bar")) | |
} | |
} label: { | |
RedrawBorder { | |
AnimatedText(text: "Push \(count)") | |
} | |
} | |
} | |
List(states) { | |
InfoView(state: $0) | |
} | |
} | |
.padding() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct RedrawBorder<Content>: View where Content: View { | |
@State private var color: Color = .red | |
private let content: () -> Content | |
init(@ViewBuilder content: @escaping () -> Content) { | |
self.content = content | |
} | |
var body: some View { | |
ZStack { | |
Rectangle() | |
.onAppear(perform: { | |
withAnimation(.linear(duration: 0.5), { | |
color = .clear | |
}) | |
}) | |
.foregroundColor(.clear) | |
.border(color, width: 5.0) | |
.ignoresSafeArea() | |
content() | |
} | |
} | |
} | |
struct AnimatedText: View { | |
@State private var color: Color = .red | |
let text: String | |
var body: some View { | |
Text(text) | |
.onAppear(perform: { | |
withAnimation(.linear(duration: 0.5), { | |
color = .clear | |
}) | |
}) | |
.border(color, width: 5.0) | |
} | |
} | |
struct InfoView: View { | |
struct State: Identifiable { | |
let id = UUID() | |
var text: String | |
var text2: String | |
var text3: String | |
} | |
let state: State | |
var body: some View { | |
let _ = print("InfoView.body") | |
VStack { | |
AnimatedText(text: state.text) | |
AnimatedText(text: state.text2) | |
AnimatedText(text: state.text3) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment