使用SwiftUI';s页面选项卡视图样式选项卡视图
使用Swift5.3.2、iOS14.4.2、XCode12.4、 我试图在SwiftUI中创建一个页面视图,使用iOS14新的使用SwiftUI';s页面选项卡视图样式选项卡视图,swiftui,tabview,pageviews,pagetabviewstyle,Swiftui,Tabview,Pageviews,Pagetabviewstyle,使用Swift5.3.2、iOS14.4.2、XCode12.4、 我试图在SwiftUI中创建一个页面视图,使用iOS14新的PageTabViewStyle作为选项卡视图 页面应能在页面之间顺利交换 只要我没有以任何方式对selectedIndex执行任何类型的onChange-操作,这一切都可以正常工作 一旦我做了.onChange(of:selectedIndex){newidx in…},那么刷卡就很慢,不再适用于可用性 但我的应用程序需要跟踪页面索引,并且需要在任何索引更改时执行代码
PageTabViewStyle
作为选项卡视图
页面应能在页面之间顺利交换
只要我没有以任何方式对selectedIndex
执行任何类型的onChange
-操作,这一切都可以正常工作
一旦我做了.onChange(of:selectedIndex){newidx in…}
,那么刷卡就很慢,不再适用于可用性
但我的应用程序需要跟踪页面索引,并且需要在任何索引更改时执行代码
如何在SwiftUI的页面视图中检测到刷卡操作??
我意识到,只要我使用任何类型的.onReceive()
或.onChange(of:)
和selectedIndex
或其任何发布者,我总是会以不再平滑的滑动手势结束
如何克服此问题并获得平滑的滑动和索引信息
这是我的密码:
struct CustomPageView: View {
@ObservedObject var myPageSelectionService = PageSelectionService()
@State private var contents = ["a","b","c"]
@State private var selectedIndex: Int = 0
var body: some View {
TabView(selection: $selectedIndex) {
ForEach(0..<contents.count, id:\.self) { i in
Text(contents[i]).tag(i)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
.onChange(of: selectedIndex) { newIndex in
print(newIndex)
}
}
}
怎么办
以下是整个应用程序(如果好奇):
import SwiftUI
enum MyState {
case waiting
case running
}
class AppState: ObservableObject {
@Published var myState: MyState = .running
}
class MyService: ObservableObject {
@Published var someServiceValue = 0
}
struct MainView: View {
@StateObject var appState = AppState()
@StateObject var myService = MyService()
var body: some View {
ParentView()
.environmentObject(appState)
.environmentObject(myService)
}
}
struct ParentView: View {
@EnvironmentObject var appState: AppState
@EnvironmentObject var myService: MyService
var body: some View {
NavigationView {
ZStack {
switch appState.myState {
case .waiting:
Text("Waiting")
case .running:
ChildView()
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
.onChange(of: myService.someServiceValue) { newValue in
if newValue == 4 {
appState.myState = .waiting
}
}
}
}
struct ChildView: View {
@EnvironmentObject var myService: MyService
@State private var contents = ["img_team_ivo","img_team_beat","img_team_benny","img_team_irfan","img_team_jenny","img_team_lukas","img_team_sabrina","img_team_nici","img_team_pascal","img_team_patrick","img_team_silvan","img_team_stephan","img_team_christoph"]
@State private var selectedIndex: Int = 0
var body: some View {
ZStack {
ScrollView {
ZStack {
TabView(selection: $selectedIndex) {
ForEach(0..<contents.count, id:\.self) { index in
ZStack {
Image(contents[index])
.resizable()
.scaledToFit()
}
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
}
}
}
.onChange(of: selectedIndex) { newIdx in
// !!!!!!!!!! app stalls - no more smooth swiping of Pages :(
myService.someServiceValue = newIdx
}
}
}
导入快捷界面
我的国家{
案件等待
案件审理
}
类AppState:ObservableObject{
@已发布的变量myState:myState=.running
}
类MyService:observeObject{
@已发布的变量someServiceValue=0
}
结构主视图:视图{
@StateObject变量appState=appState()
@StateObject变量myService=myService()
var body:一些观点{
父视图()
.environmentObject(appState)
.environmentObject(myService)
}
}
结构父视图:视图{
@EnvironmentObject变量appState:appState
@环境对象变量myService:myService
var body:一些观点{
导航视图{
ZStack{
开关appState.myState{
案件.等候:
文本(“等待”)
案例.运行:
ChildView()
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
.onChange(of:myService.someServiceValue){newValue in
如果newValue==4{
appState.myState=.waiting
}
}
}
}
结构子视图:视图{
@环境对象变量myService:myService
@国家私人var内容=[“img_团队ivo”、“img_团队beat”、“img_团队benny”、“img_团队irfan”、“img_团队jenny”、“img_团队lukas”、“img_团队sabrina”、“img_团队nici”、“img_团队pascal”、“img_团队patrick”、“img_团队silvan”、“img_团队stephan”、“img_团队christoph”]
@状态私有变量selectedIndex:Int=0
var body:一些观点{
ZStack{
滚动视图{
ZStack{
选项卡视图(选择:$selectedIndex){
ForEach(0。。
import SwiftUI
enum MyState {
case waiting
case running
}
class AppState: ObservableObject {
@Published var myState: MyState = .running
}
class MyService: ObservableObject {
@Published var someServiceValue = 0
}
struct MainView: View {
@StateObject var appState = AppState()
@StateObject var myService = MyService()
var body: some View {
ParentView()
.environmentObject(appState)
.environmentObject(myService)
}
}
struct ParentView: View {
@EnvironmentObject var appState: AppState
@EnvironmentObject var myService: MyService
var body: some View {
NavigationView {
ZStack {
switch appState.myState {
case .waiting:
Text("Waiting")
case .running:
ChildView()
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
.onChange(of: myService.someServiceValue) { newValue in
if newValue == 4 {
appState.myState = .waiting
}
}
}
}
struct ChildView: View {
@EnvironmentObject var myService: MyService
@State private var contents = ["img_team_ivo","img_team_beat","img_team_benny","img_team_irfan","img_team_jenny","img_team_lukas","img_team_sabrina","img_team_nici","img_team_pascal","img_team_patrick","img_team_silvan","img_team_stephan","img_team_christoph"]
@State private var selectedIndex: Int = 0
var body: some View {
ZStack {
ScrollView {
ZStack {
TabView(selection: $selectedIndex) {
ForEach(0..<contents.count, id:\.self) { index in
ZStack {
Image(contents[index])
.resizable()
.scaledToFit()
}
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
}
}
}
.onChange(of: selectedIndex) { newIdx in
// !!!!!!!!!! app stalls - no more smooth swiping of Pages :(
myService.someServiceValue = newIdx
}
}
}