gpt4 book ai didi

swiftui - 如何为 SwiftUI 制作滑动手势?

转载 作者:行者123 更新时间:2023-12-04 09:48:05 24 4
gpt4 key购买 nike

SwiftUI 中是否有简单的手势识别方法?

根据这个tutorial我根据我的情况复制了代码:

(我需要从另一个 View 更改值“page2”)

import SwiftUI

struct SwipeGesture: UIViewRepresentable {

@Binding var page2: Int

func makeCoordinator() -> SwipeGesture.Coordinator {
return SwipeGesture.Coordinator(parent1: self)
}

func makeUIView(context: UIViewRepresentableContext<SwipeGesture>) -> UIView {
let view = UIView()
view.backgroundColor = .clear
let left = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.left))
left.direction = .left

let right = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.right))
right.direction = .right

view.addGestureRecognizer(left)
view.addGestureRecognizer(right)
return view
}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<SwipeGesture>) {

}

class Coordinator: NSObject{

var parent : SwipeGesture

init(parent1 : NSObject){
parent = parent1
}

@objc func left(){
print("left swipe")
parent.page2 = parent.page2 + 1
}

@objc func right(){
print("right swipe")
parent.page2 = parent.page2 - 1
}
}
}

在 ContentView 中使用: SwipeGesture(page2: $page2)
@State var page = 0 

用于主 View
@Binding var page: Int

是为了这个 SwipeGesture struct问题是: enter image description here

任何想法我做错了什么?

SwiftUI 代码:
var body: some View {
// MARK: - Screen Layers
ZStack {

// Logo name
VStack {
Spacer()
.frame(height: self.margin1)
Image(uiImage: UIImage(named: "lobby_bg-logo")!)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: self.screenH * 0.3125)
Spacer()
}
SwipeGesture(page2: $page2)

错误:

Cannot assign value of type 'NSObject' to type 'SwipeGesture' Occurs in init part.

最佳答案

这是修复

class Coordinator: NSObject{

var parent : SwipeGesture

init(parent1 : SwipeGesture){ // << corrected type !!
parent = parent1
}

关于swiftui - 如何为 SwiftUI 制作滑动手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62051221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com