UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Get background color of tap location

Forums > SwiftUI

I am trying to get the background color of the location where I tapped on the screen.

If I understood correctly the location I can get by using the dragGesture. But I have trouble getting the background color of that location.

Any sugestions?

1      

I would try using a combination of these two techniques that Paul described:

How to detect the location of a tap inside a view - a free SwiftUI by Example tutorial

How to convert a SwiftUI view to an image - a free SwiftUI by Example tutorial

So the idea would be to 1) detect the location of the tap, and then 2) render the screen to an image.

Then, 3) use this gist to get the color of the pixel at the location in the image:

UIView extension which allows to get colour of any point inside

Do note that I haven't actually tried this yet, so it may not work. Worth a shot, though.

Depending on your exact use case, you should adjust this approach accordingly.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.