jsb*_*eJS 7 image swift swiftui
我有一个关于图像上的 tapAction 的问题。在不应发生的剪切区域上调用 TapAction 闭包。我该怎么办?
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: 200, alignment: .center)
.presentation(tapped ? Modal(Image(uiImage: image)) : nil)
.clipped()
.cornerRadius(10)
.border(Color.black, width: 2, cornerRadius: 10)
.tapAction {
self.tapped.toggle()
}
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
kon*_*iki 21
更新
我更新了我的答案。这是正确的做法。有一个称为修饰符的修饰符contentShape(),您可以使用它来定义命中测试区域:
import SwiftUI
struct ContentView: View {
@State private var tapped = false
var body: some View {
Image(systemName: "circle.fill")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: 200, alignment: .center)
.presentation(tapped ? Modal(Image(systemName: "photo")) : nil)
.clipped()
.cornerRadius(10)
.border(Color.black, width: 2, cornerRadius: 10)
.contentShape(TapShape())
.tapAction {
self.tapped.toggle()
}
}
struct TapShape : Shape {
func path(in rect: CGRect) -> Path {
return Path(CGRect(x: 0, y: 0, width: rect.width, height: 200))
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
528 次 |
| 最近记录: |