如何让按钮执行在其“父”视图中触发功能的操作?我正在尝试重构我的代码,以便组件尽可能小。
在这种情况下,按钮执行一些任务,但其中之一是运行一个函数:
Button(
action: {
self.setViewBackToNil()
}){
Text("Button")
}
// which triggers a function
func setViewBackToNil(){
self.userData.image = nil
self.isProcessing = false
.... etc
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我将按钮变成它自己的视图,我不能传递 self.setViewBackToNil 因为它包含在父级的结构中。
组件有没有办法触发其父级中的函数?
我有一个视图,它使用 Geometry Reader 来计算图像区域应该有多大:
GeometryReader { metrics in
ZStack{
self.image
.resizable()
.frame(width: (metrics.size.height - 10) * 0.561403509 , height: metrics.size.height - 10, alignment: .top)
.clipped()
}
}
Run Code Online (Sandbox Code Playgroud)
但是我有一个函数,我想使用 GeometryReader 计算的框架高度和宽度来裁剪图像。
我有一个单独的功能,可以在按下按钮时裁剪图像:
DownloadImageButton(prepareImagefunction: { self.prepareImage() })
Run Code Online (Sandbox Code Playgroud)
然后调用一个 prepareImage 函数:
func prepareImage( ) {
// In order for image cropping to work, we need the equivalent of view.bounds in order to adjust the crop so that it correctly measures the crop area. We need metrics.width and metrics.height
var adjustmentScaleForDisplaySize = targetSize.width / …Run Code Online (Sandbox Code Playgroud) UIViewControllerRepresentable 从 ViewController 接收图像。此图像应可用于 SwiftUI 视图。
可观察对象:
class CurrentImage: ObservableObject {
var didChange = PassthroughSubject<Void, Never>()
@Published var photoTaken: UIImage? = nil {didSet{didChange.send()}}
}
Run Code Online (Sandbox Code Playgroud)
UIViewControllerRepresentable:
struct CameraPreviewView: UIViewControllerRepresentable {
var currentImage: CurrentImage
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
// Notification receives a copy of the image taken from the ViewController:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
if let image = notification.userInfo?["image"] as? …Run Code Online (Sandbox Code Playgroud) 使用 App Clip 创建了一个应用程序。App Clip URL 显示为已验证。Connect上的应用信息中已经设置了一张基本卡。
在 TestFlight 中设置了一个调用 URL。仅从 TestFlight 下载了 App Clip。
当使用代表 URL 的二维码时,它会调用“Web 链接”弹出窗口而不是应用程序剪辑弹出窗口。
如何通过 TestFlight 测试查看 App Clip 卡片?我觉得我要么遗漏了什么,要么在 TestFlight 中不可能。