我在iOS上有十年的经验,但对Swift/SwiftUI来说还是新手,所以请温柔一点.有一件事让我感到困惑,那就是为什么如果不透明度设置为零或设置为0,视图(我try 了一些,但让我们使用TextView()为例)似乎不会被绘制.背景( colored颜色 .清晰).

我试图实现的是在HStack中放置两个按钮,作为ZStack中的第一个成员(所以"后退"最多),以确定用户何时双击屏幕的左侧或右侧,但我不希望实际显示任何内容,只需检测点击即可.我的黑客解决方案是将不透明度设置为0.001.在不透明度设置下,什么都看不见,我可以用它来检测.OnTap手势(计数:2),但这肯定不是最佳方式.在UIKit land中,我很想用屏幕尺寸来处理点击坐标,以确定它落在哪一侧,但我不认为这是非常"快速"的,即使不包装UIKit类也是可能的.努力学习,尽可能做到"纯快捷".

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

我想我的问题是:

a) 如何使用隐形但仍被布置的手势来检测?

b) 如果不透明度为零或背景设置为"清除",为什么Text()和其他视图基本上不存在?

谢谢

推荐答案

a) 如何使用隐形但仍被布置的手势来检测?

我们可以使用.contentShape修改器使任何区域,甚至完全透明,可点击(又名命中可测试),如:

    Color.clear
        .frame(width: 100, height: 100)
        .contentShape(Rectangle())
        .onTapGesture {
            print(">>> Tapped!")
        }

b) 如果不透明度为零或背景设置为"清除",为什么Text()和其他视图基本上不存在?

SwiftUI将视图渲染到一个上下文中(它与UIKit相反,在UIKit中,每个UIView视图都会渲染自身),因此当视图完全透明时,就没有任何可渲染的内容.

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

自定义alert 未执行任何操作

SwiftUI@Observable不跟踪父类属性中的更改以更新视图

在iOS中禁用URLSession自动重试机制

如何在iOS 17中拦截UITextView中的链接点击?UITextItemInteraction已弃用

Flutter 翼项目.升级到XCode 15.0.1`pod install`抛出错误FlutterMacOS.podspec.json 3.13.2的ParserError

如何在SwiftUI中将图像整合到文本标签中间?

拖动手势导致 sim 崩溃 Swift UI

Swift中是否有一种方法可以为可选属性创建一个计算(computed)属性

表视图(SwiftUI)中的多列?

如何擦除 Swift 中的 Core Graphics Context 绘图?

错误:无法根据成员environmentObject推断上下文基础

通过隐藏和显示视图在 iMessage 应用程序中使用不同的视图

.overlay 和 body 是什么 swift 概念

如何在通用应用程序中同时支持 iPad 和 iPhone 视网膜图形

Sierra 中的安全/协同设计: keys 串忽略访问控制设置和 UI 提示权限

NSURLSession:如何增加 URL 请求的超时时间?

如何在 Android 和 iPhone 的移动应用程序中实施推荐计划

如何为 NSDate 添加一个月?

openURL:在 iOS 10 中已弃用