我正尝试像以前使用EAIntroView在Objective-C中所做的那样向我的SwiftApp添加简介:
https://github.com/ealeksandrov/EAIntroView
我是第一次使用SwiftUI,但我有很多不足。此时,我的应用程序将显示如下主视图:
var body: some View {
Group {
if mainhomeMode == .mylists {
MyLists()
.onReceive(publisher) { (payload) in
self.toggleMainView()
}
} else {
CarsHome()
.onReceive(publisher) { (payload) in
self.toggleMainView()
}
}
}
}
这是在应用加载时显示MyLists()或CarsHome()的快速视图中的主体,但是在应用首次加载时,我需要加载一个介绍视图,或者在按钮点击时显示相同的介绍视图用户想要多次。
我如何使用几个滚动视图(例如EAIntroView)来实现介绍控制器?
答案 0 :(得分:1)
您需要使用从View
继承的UIViewRepresentable
:
https://developer.apple.com/documentation/swiftui/uiviewrepresentable
表示UIKit视图的视图。
import SwiftUI
import EAIntroView
struct ContentView: UIViewRepresentable {
let sampleDescription1 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
let sampleDescription2 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore."
let sampleDescription3 = "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem."
let sampleDescription4 = "Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit."
class Coordinator: NSObject, EAIntroDelegate {
var parent: ContentView
init(_ parent: ContentView) {
self.parent = parent
}
func introDidFinish(_ introView: EAIntroView!, wasSkipped: Bool) {
if (wasSkipped) {
print("Intro skipped")
} else {
print("Intro finished")
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> EAIntroView {
let view = UIView(frame: UIScreen.main.bounds)
let page1 = EAIntroPage()
page1.title = "Hello world"
page1.desc = sampleDescription1
page1.bgImage = UIImage(named: "bg1")
page1.titleIconView = UIImageView(image: UIImage(named: "title1"))
let page2 = EAIntroPage()
page2.title = "This is page 2"
page2.desc = sampleDescription2
page2.bgImage = UIImage(named: "bg2")
page2.titleIconView = UIImageView(image: UIImage(named: "title2"))
let page3 = EAIntroPage()
page3.title = "This is page 3"
page3.desc = sampleDescription3
page3.bgImage = UIImage(named: "bg3")
page3.titleIconView = UIImageView(image: UIImage(named: "title3"))
let page4 = EAIntroPage()
page4.title = "This is page 4"
page4.desc = sampleDescription4
page4.bgImage = UIImage(named: "bg4")
page4.titleIconView = UIImageView(image: UIImage(named: "title4"))
let intro = EAIntroView(frame: view.bounds, andPages: [page1, page2, page3, page4]);
intro?.delegate = context.coordinator
intro?.skipButtonAlignment = .center
intro?.skipButtonY = 80
intro?.pageControlY = 42
intro?.show(in: view, animateDuration: 0.3)
view.addSubview(intro!)
return intro!
}
func updateUIView(_ view: EAIntroView, context: Context) {
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
注意::您将需要从示例项目文件夹here中导入图像。