Soy un veterano de iOS de una década pero nuevo en Swift/SwiftUI, así que sea amable. Una cosa que me confunde es por qué una Vista (he probado algunas, pero usemos TextView() por ejemplo) parece no dibujarse si la opacidad se establece en cero o .background (Color.clear).
Lo que estoy tratando de lograr es poner dos botones en un HStack como el primer miembro en un ZStack (así que "atrás" más) para determinar cuándo el usuario toca dos veces el lado izquierdo de la pantalla o el derecho, pero yo no quiero que se muestre nada, solo detecte los toques. Mi solución hackish es establecer la opacidad en 0.001. Con esa configuración de opacidad, nada es visible y puedo detectar con .onTapGesture (count: 2), pero seguramente esta no es la forma óptima. En UIKit land, me sentiría tentado a procesar la coordenada de toque yo mismo usando las dimensiones de la pantalla para determinar de qué lado aterrizó, pero no creo que sea terriblemente "rápido", incluso si es posible sin envolver las clases de UIKit. Tratando de aprender, haciendo todo lo más "puro SwiftUI" posible.
public var body: some View { ZStack { HStack { VStack { Text("").frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.blue) .opacity(0.0001) //hack workaround, setting opacity to 0.0 and it's like the view isn't even drawn or present (the onTapGesture doesn't get recognized because the VStack has no size when the Text frame is zero and that's the only point! Interestingly setting the background to Color.clear creates the same issue }.onTapGesture(count: 2) { print("got a double tap on the LEFT side scroll button") } VStack { Text("").frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.yellow) .opacity(0.0001)//hack workaround, setting opacity to 0.0 and it's like the view isn't even drawn or present (the onTapGesture doesn't get recognized because the VStack has no size when the Text frame is zero and that's the only point! Interestingly setting the background to Color.clear creates the same issue }.onTapGesture(count: 2) { print("got a double tap on the RIGHT side scroll button") } } [subsequent members of the ZStack removed as they're not relevant to the question] } }Supongo que mis preguntas son:
a) ¿Qué usar que es invisible pero aún está diseñado para detectar onTapGesture?
b) algún conocimiento sobre por qué Text() y otras vistas básicamente no existen si la opacidad es cero o el fondo está configurado para borrar?
¡Gracias!
a) ¿Qué usar que es invisible pero aún está diseñado para detectar onTapGesture?
Podemos usar el modificador .contentShape para hacer que cualquier área, incluso completamente transparente, se pueda tocar (también conocida como hit-testable), como:
Color.clear .frame(width: 100, height: 100) .contentShape(Rectangle()) .onTapGesture { print(">>> Tapped!") }b) algún conocimiento sobre por qué Text() y otras vistas básicamente no existen si la opacidad es cero o el fondo está configurado para borrar?
SwiftUI representa las vistas en un contexto (es opuesto a UIKit, donde cada vista de UIView se representa a sí misma), por lo que cuando la vista es completamente transparente, no hay nada que representar.