Last active
August 11, 2023 08:27
-
-
Save JRR-OSU/667dc80e85a860726fd4c1ed56b31c07 to your computer and use it in GitHub Desktop.
SwiftUI Crossfade and Flip Shader Example
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
import SwiftUI | |
struct DemoView: View { | |
@State var effectValue: Double | |
let end = 2.75 | |
init() { | |
self._effectValue = State(initialValue: end) | |
} | |
var body: some View { | |
VStack { | |
Slider(value: $effectValue, in: 0.3...end) { | |
EmptyView() | |
}.padding() | |
ZStack { | |
TimelineView(.animation(minimumInterval: 1 / 120)) { context in | |
ZStack { | |
WeatherCard1(currentTime: context.date) | |
WeatherCard2(currentTime: context.date) | |
.visualEffect { content, proxy in | |
content | |
.layerEffect(ShaderLibrary.fadeAndFlip( | |
.float(effectValue), | |
.float2(proxy.size)), maxSampleOffset: proxy.size) | |
} | |
} | |
} | |
} | |
.drawingGroup() | |
.frame(width: 250, height: 250) | |
.clipShape(RoundedRectangle(cornerRadius: 8)) | |
.onTapGesture { | |
withAnimation(.interpolatingSpring(duration: 1.5)) { | |
reverse() | |
} | |
} | |
} | |
} | |
func reverse() { | |
effectValue = effectValue == end ? 0.3 : end | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment