Skip to content

Commit

Permalink
Load image when appear when need force transition
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Oct 3, 2021
1 parent ef39a51 commit 7755800
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Demo/Demo/Kingfisher-Demo/SwiftUIViews/SingleViewDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct SingleViewDemo : View {

@State private var index = 1
@State private var blackWhite = false
@State private var forceTransition = true

var url: URL {
URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/Loading/kingfisher-\(self.index).jpg")!
Expand All @@ -52,7 +53,7 @@ struct SingleViewDemo : View {
ProgressView(progress)
}
.fade(duration: index == 1 ? 0 : 1) // Do not animate for the first image. Otherwise it causes an unwanted animation when the page is shown.
.forceTransition()
.forceTransition(forceTransition)
.resizable()
.frame(width: 300, height: 300)
.cornerRadius(20)
Expand All @@ -65,6 +66,8 @@ struct SingleViewDemo : View {
Button(action: {
self.blackWhite.toggle()
}) { Text("Black & White") }
Toggle("Force Transition?", isOn: $forceTransition)
.frame(width: 300)

}.navigationBarTitle(Text("Basic Image"), displayMode: .inline)
}
Expand Down
11 changes: 10 additions & 1 deletion Sources/SwiftUI/KFImageRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
let context: KFImage.Context<HoldingView>

var body: some View {
binder.start(context: context)
if !context.options.forceTransition {
binder.start(context: context)
}

return ZStack {
context.configurations
.reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
Expand All @@ -52,6 +55,12 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
Color.clear
}
}
.onAppear { [weak binder = self.binder] in
guard let binder = binder else {
return
}
binder.start(context: context)
}
.onDisappear { [weak binder = self.binder] in
guard let binder = binder else {
return
Expand Down

0 comments on commit 7755800

Please sign in to comment.