Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kingfisher image elements untappable on iOS 18 #2295

Closed
3 tasks done
danieldaquino opened this issue Sep 21, 2024 · 4 comments · Fixed by #2296
Closed
3 tasks done

Kingfisher image elements untappable on iOS 18 #2295

danieldaquino opened this issue Sep 21, 2024 · 4 comments · Fixed by #2296

Comments

@danieldaquino
Copy link

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

There seems to be a regression on the tapping detection of Kingfisher image elements (We are using KFAnimatedImage) on SwiftUI caused by the introduction of iOS 18.

What

We have found that KFAnimatedImage is no longer recognizing onTapGesture on its own on iOS 18

Reproduce

Device: Probably any iOS device (I used iPhone 13 mini and iPhone SE simulator)
iOS: 18.0 (I also used iOS 17.5 as a baseline)
Kingfisher: I am using 8.0.1 (Also reproducible on some 7.x versions)
Xcode: I am using Xcode 16.0 (16A242d)

I wrote a minimal example that reproduces the problem:

import SwiftUI
import Kingfisher

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            TapGestureExampleKFAnimatedImage()
            TapGestureExampleAsyncImage()
        }
    }
}

struct TapGestureExampleKFAnimatedImage: View {
    @State var tapCount = 0
    let url = URL(string: "https://images.unsplash.com/photo-1722359945617-5cd0ea1a628f?q=80&w=1587&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D")!

    var body: some View {
        VStack {
            Text("KFAnimatedImage")
                .bold()
            KFAnimatedImage(url)
                .aspectRatio(contentMode: .fit)
                .frame(width: 200, height: 200)
                .onTapGesture {
                    tapCount += 1
                }
            Text("Tap count: \(tapCount)")
        }
    }
}

struct TapGestureExampleAsyncImage: View {
    @State var tapCount = 0
    let url = URL(string: "https://images.unsplash.com/photo-1722359945617-5cd0ea1a628f?q=80&w=1587&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D")!

    var body: some View {
        VStack {
            Text("AsyncImage")
                .bold()
            AsyncImage(url: url) { image in
                image.resizable()
            } placeholder: {
                ProgressView()
            }
            .aspectRatio(contentMode: .fit)
            .frame(width: 200, height: 200)
            .onTapGesture {
                tapCount += 1
            }
            Text("Tap count: \(tapCount)")
        }
    }
}

#Preview {
    ContentView()
}

Here is the behavior, side-by-side, between running this on iOS 17.5 and iOS 18:

iOS 17.5 iOS 18.0
ios 17 video ios 18 video

As shown in the videos above, KFAnimatedImage detects onTapGesture elements on iOS 17.5, but not on iOS 18.

Other Comment

I found this comment on a separate issue: #2046 (comment)

and applying a similar patch as follows:

struct TapGestureExampleKFAnimatedImage: View {
    @State var tapCount = 0
    let url = URL(string: "https://images.unsplash.com/photo-1722359945617-5cd0ea1a628f?q=80&w=1587&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D")!

    var body: some View {
        VStack {
            Text("KFAnimatedImage")
                .bold()
            KFAnimatedImage(url)
                .aspectRatio(contentMode: .fit)
                .frame(width: 200, height: 200)
+                 .contentShape(Rectangle())
                .onTapGesture {
                    tapCount += 1
                }
            Text("Tap count: \(tapCount)")
        }
    }
}

does seem to solve the issue. However, I am concerned that this workaround is a bit counter-intuitive to use on every usage and may lead to hard-to-debug issues in the future. Is there a way we can fix this within KFAnimatedImage?

danieldaquino added a commit to danieldaquino/damus that referenced this issue Sep 21, 2024
The introduction of iOS 18 brought a new bug that made `KFAnimatedImage`
not recognize tap gestures and become unclickable. (onevcat/Kingfisher#2295)

This commit addresses the issue with a workaround found here:
onevcat/Kingfisher#2046 (comment)

The workaround was suggested by the author of the library to fix a
slightly different issue, but that property seems to work for our
purposes.

The issue is addressed by adding a `contentShape` property to usages
of `KFAnimatedImage`, in order to make them clickable. A custom modifier
was created to make the solution less obscure and more obvious.

Furthermore, one empty tap gesture handler was removed as it was
preventing other tap gesture handlers on the image carousel from being
triggered on iOS 18

Testing
-------

PASS

Configurations:
- iPhone 13 mini on iOS 18.0
- iPhone SE simulator on iOS 17.5
Damus: This commit
Coverage:
- Check that the following views are clickable:
    - Images in the carousel
    - Profile picture on notes
    - Profile picture on thread comments
    - Profile picture on profile page

Changelog-Fixed: Fix items that became unclickable on iOS 18
Closes: damus-io#2342
Closes: damus-io#2370
Signed-off-by: Daniel D’Aquino <[email protected]>
danieldaquino added a commit to danieldaquino/damus that referenced this issue Sep 21, 2024
The introduction of iOS 18 brought a new bug that made `KFAnimatedImage`
not recognize tap gestures and become unclickable. (onevcat/Kingfisher#2295)

This commit addresses the issue with a workaround found here:
onevcat/Kingfisher#2046 (comment)

The workaround was suggested by the author of the library to fix a
slightly different issue, but that property seems to work for our
purposes.

The issue is addressed by adding a `contentShape` property to usages
of `KFAnimatedImage`, in order to make them clickable. A custom modifier
was created to make the solution less obscure and more obvious.

Furthermore, one empty tap gesture handler was removed as it was
preventing other tap gesture handlers on the image carousel from being
triggered on iOS 18

Testing
-------

PASS

Configurations:
- iPhone 13 mini on iOS 18.0
- iPhone SE simulator on iOS 17.5
Damus: This commit
Coverage:
- Check that the following views are clickable:
    - Images in the carousel
    - Profile picture on notes
    - Profile picture on thread comments
    - Profile picture on profile page

Changelog-Fixed: Fix items that became unclickable on iOS 18
Closes: damus-io#2342
Closes: damus-io#2370
Signed-off-by: Daniel D’Aquino <[email protected]>
@onevcat
Copy link
Owner

onevcat commented Sep 21, 2024

Thanks for reporting this.

It seems this issue is a regression in iOS 18, likely due to Apple rewriting some gesture-related code as they advertised. The simplest example to reproduce the problem is:

struct ContentView: View {
    @State private var count = 0
    var body: some View {
        VStack {
            Text("Count: \(count)")
            MyUIView()
                .frame(width: 200, height: 200)
                .onTapGesture { count += 1 }
        }
        .padding()
    }
}

struct MyUIView: UIViewRepresentable {
    func updateUIView(_ uiView: UIViewType, context: Context) { }
    func makeUIView(context: Context) -> some UIView {
        UIImageView(image: UIImage(systemName: "person.crop.circle"))
    }
}

To align the behavior with iOS 17 and earlier, the following adjustment is the simplest solution:

func makeUIView(context: Context) -> some UIView {
-   UIImageView(image: UIImage(systemName: "person.crop.circle"))
+   let v = UIImageView(image: UIImage(systemName: "person.crop.circle"))
+   v.isUserInteractionEnabled = true
+   return v
}

The contentShape added to KFAnimatedImage seems to have a side effect of enabling user interaction on the image view. So it just happened to "fix" the issue.

I can’t confirm if this is intended behavior by Apple, but given that KFAnimatedImage is essentially a wrapper around UIImageView in UIKit, it now respects the isUserInteractionEnabled property (which is false in fact) in iOS 18.

As a workaround, I’ll apply this change to KFAnimatedImage by setting the isUserInteractionEnabled to true to restore behavior similar to earlier iOS versions. It also allows us to share the same behavior between a normal KFImage and its animated version KFAnimatedImage.

If the users wants a "click-through" behavior, they can apply a .allowsHitTesting(false) modifier for the purpose.

@onevcat
Copy link
Owner

onevcat commented Sep 21, 2024

Fixed in 8.0.3.

@danieldaquino
Copy link
Author

Thank you @onevcat!

@ChungPhanNgoc
Copy link

ChungPhanNgoc commented Oct 15, 2024

Hello all.
Now I'm having the problem that the images are no longer animated. Maybe the problem is with iOS 18. Anyone have a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants