Skip to content

Instantly share code, notes, and snippets.

@jackpal
Created February 17, 2020 05:29
Show Gist options
  • Save jackpal/156742dd6ef641d9dd6852945f466812 to your computer and use it in GitHub Desktop.
Save jackpal/156742dd6ef641d9dd6852945f466812 to your computer and use it in GitHub Desktop.
A SwiftUI slideshow view
struct TVSlideshowView : View {
let publisher : AnyPublisher<UIImage?, Never>
@State private var uiImageA: UIImage? = nil
@State private var uiImageB: UIImage? = nil
@State private var imageAHasPriority: Bool = true
var body: some View {
ZStack {
decorate(uiImage: uiImageA, isImageA: true)
decorate(uiImage: uiImageB, isImageA: false)
}
.onAppear() {
UIApplication.shared.isIdleTimerDisabled = true
}
.onDisappear() {
UIApplication.shared.isIdleTimerDisabled = false
}
.onReceive(publisher) { uiImage in
self.updateImages(uiImage:uiImage)
}
}
func decorate(uiImage: UIImage?, isImageA: Bool) -> some View {
Group {
if uiImage != nil {
decorate(uiImage: uiImage!, isImageA: isImageA)
}
}
}
func decorate(uiImage: UIImage, isImageA: Bool) -> some View {
GeometryReader { proxy in
Image(uiImage:uiImage)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width:proxy.size.width, height:proxy.size.height)
.animation(nil)
.opacity(self.imageAHasPriority == isImageA ? 1.0 : 0.0)
.animation(.easeOut(duration:0.6))
}
}
func updateImages(uiImage: UIImage?) {
if uiImage != nil {
imageAHasPriority.toggle()
if imageAHasPriority {
uiImageA = uiImage
} else {
uiImageB = uiImage
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment