如何解决如何使用 iOS14 TabView 的 PageTabViewStyle() 页面滑动检测滑动并对其执行操作?
使用 Swift5.3.2、iOS14.4.2、XCode12.4,
我正在为在 SwiftUI 中创建页面视图这一新的 iOS14 功能而苦苦挣扎。
一切正常,只是我无法检测到滑动的时刻!
我尝试使用 .onChange(selectionIndex)
,但是一旦我为 TabView 的 selectionIndex
创建了这个更改侦听器,滑动就会停止。不再顺畅。
struct MyView: View {
@State var selectionIndex = 0
var body: some View {
TabView(selection: $selectionIndex) {
ForEach(mediaList.paths.indices,id: \.self) { index in
SomePageView(media: mediaList.paths[index])
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.onChange(of: selectionIndex) { newIdx in
customDotsService.selectedIndex = newIdx // because TabView's PageViewStyle does not have little dots that can be placed somewhere else on screen...
sendCommand(index: newIdx) // some background task to send commands to other services...
}
}
}
即使我的 onChange(selectionIndex) { newIdx in ... }
完全为空,滑动也会停止并且内存进入循环。
这真的是一个很烦人的问题,我从 4 天起就一直在处理这个问题。
非常感谢任何帮助。
我需要从 Apple 获得什么:一个工作的 PageView,它具有正确的生命周期方法 .onAppear()
、.onChange(selectionIndex)
及其可以放置在屏幕上任何位置的小点指示器。帮助宇宙,帮助我们在 SwiftUI 中得到这个。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。