TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Canvas TapGestures HitAreas or Alternative

Forums > SwiftUI

New to SwiftUI to open to any alternative suggests on the recommended way.

I'm creating a game which has a generated background (not static image) with clickable images on top. How should the project be constructed?

1) Version 1: I started off using Views to layout the background (think tiling shapes) such as Chess. I then need to add images on top in various locations sometimes overlapping the tiles. I need to know the location of the tiles to place in exact locations on edges, corners, center of the background, which I didn't have using this method. The top View needs to know positional information of the bottom generated View, which can change based on orientation and device size.

2) Version 2: Moved to a Canvas to have specific control of positions and layout. I create Views containing Images for the "clickable" objects which are then resolvedSymbols and added to the graphics context.

The problem I am facing with the Canvas is the tap gestures for the Views (Images, HStacks) do not work. I assume that using symbols removes that functionality. Therefore, it seems that onChanged .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local) on the Canvas is my only solution for capturing user interaction. This then involves Hit testing the symbols I added based of the tap location. I know them of course, I just need to loop over them testing each one.

Before I start (hit testing) I want to check that I am not reinventing the wheel or not following guidelines.

Please advise either:

  • Version1 with improved layout of top view or
  • Version2 hit testing on canvas (a common thing, if this is the only way?)

Thanks

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

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.