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

onTapGesture event is confused in nested ScrollView #2046

Closed
3 tasks done
opleonnn opened this issue Mar 15, 2023 · 5 comments
Closed
3 tasks done

onTapGesture event is confused in nested ScrollView #2046

opleonnn opened this issue Mar 15, 2023 · 5 comments

Comments

@opleonnn
Copy link

Check List

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

Issue Description

KFImage onTapGesture event is confused in nested ScrollView

What

I used KFImage in the nested ScrollView, and add onTapGesture event to printing index for every KFImage, and the wrong index will be printed.

Reproduce

ScrollView {
            VStack {
                ScrollView(.horizontal) {
                    HStack(spacing: 15) {
                        ForEach(0..<3) { index in
                            KFImage(URL(string: imageURLs[index]))
                                .resizable()
                                .scaledToFill()
                                .frame(width: 300, height: 400)
                                .clipped()
                                .onTapGesture {
                                    print(index)
                                }
                        }
                    }
                }
                .background(Color.yellow)
            }
        }
}

click KFImage in turn, printed 1, 1, 2.

@onevcat
Copy link
Owner

onevcat commented May 18, 2023

@opleonnn

I played with this code and it prints out exactly "0, 1, 2" to me.

截屏2023-05-18 23 15 55

So can it be a SwiftUI version or iOS system version issue?

I tried on Xcode 14.3 with an iPhone 14 Pro simulator with iOS 16.4. Can you verify the behavior too? Or do you have any comments on the reproducible SDK/device versions?

@opleonnn
Copy link
Author

@onevcat
Thank you for your test!
I may have found the cause, the problem is that the url of my image is not standard. I'm not sure if Kingfisher should be compatible with these errors, if not, please close this issue.

These are the image URLs I used for testing:

let imageURLs = [
    "https://static.freebeatfit.com/course/image/2d3e0b78e2ae4bacb984c388ae66d54e.15",
    "https://static.freebeatfit.com/course/image/9147882e1ab9460495f8e52edc9beaa5.03",
    "https://static.freebeatfit.com/course/image/bfbebb6ff9214719885a561953db947f.jpeg"
]

@onevcat
Copy link
Owner

onevcat commented May 19, 2023

It is quite surprising! Let me check it later.

@onevcat
Copy link
Owner

onevcat commented May 19, 2023

OK, it is a notorious thing that related to the image size (difference between its visual size and its content size)

I suggest you insert a contentShape to allow SwiftUI to have the correct tapping detecting size. If I am correct, this should work:

KFImage(URL(string: imageURLs[index]))
    .resizable()
    .scaledToFill()
    .frame(width: 300, height: 400)
    .clipped()
+   .contentShape(Rectangle())
    .onTapGesture {
        print(index)
    }

@opleonnn
Copy link
Author

Interesting. I thought it was only effective for Spacer().
Thanks for your patience! @onevcat

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]>
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

No branches or pull requests

2 participants