Compare commits

...

5 Commits

Author SHA1 Message Date
Manuel Lorenze 44a50e5f76 add TabView solution into PagerTabStripView 2023-03-10 09:21:49 -03:00
Manuel Lorenze 42d7b3e5c9 minor change 2023-03-10 07:27:05 -03:00
Manuel Lorenze 920e25f08b improve content implementation 2023-03-10 06:48:02 -03:00
Manuel Lorenze 1107cef572 add tag value in print 2023-03-09 21:03:58 -03:00
Manuel Lorenze 5297cf65dd calculate offset working with TabView 2023-03-09 17:06:31 -03:00
4 changed files with 98 additions and 12 deletions

View File

@ -536,9 +536,9 @@
CODE_SIGN_ENTITLEMENTS = Example.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_INJECT_BASE_ENTITLEMENTS = YES;
CODE_SIGN_STYLE = Manual;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 4.0.0;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 6F2G55XL63;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = iOS/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
@ -566,9 +566,9 @@
CODE_SIGN_ENTITLEMENTS = Example.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_INJECT_BASE_ENTITLEMENTS = YES;
CODE_SIGN_STYLE = Manual;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 4.0.0;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 6F2G55XL63;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = iOS/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;

View File

@ -17,7 +17,7 @@ private struct PageItem: Identifiable {
}
struct TwitterView: View {
@State var selection = 4
@State var selection = 1
@State var toggle = true
@State var swipeGestureEnabled = true

View File

@ -524,11 +524,11 @@
28F828921C494B2C00330CF4 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Manual;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 4.0.0;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 6F2G55XL63;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
@ -556,11 +556,11 @@
28F828931C494B2C00330CF4 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Manual;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 4.0.0;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 6F2G55XL63;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";

View File

@ -90,6 +90,83 @@ private struct WrapperPagerTabStripView<SelectionType, Content>: View where Sele
self.content = content()
}
@MainActor public var body: some View {
GeometryReader { geometryProxy in
TabView(selection: $selection) {
content
.overlay(
GeometryReader { geo in
Color.clear
.preference(key: ScrollViewOffsetPreferenceKey.self, value: geo.frame(in: .named("PagerView")).minX)
}
)
.onPreferenceChange(ScrollViewOffsetPreferenceKey.self, perform: { offset in
print("Debug -> selection: \(selection), offset: \(offset)")
})
}
.tabViewStyle(PageTabViewStyle.page(indexDisplayMode: .never))
.coordinateSpace(name: "PagerView")
.gesture(swipeGestureEnabled && swipeOn ?
DragGesture(minimumDistance: 25).onChanged { value in
swipeOn = !(edgeSwipeGestureDisabled.contains(.left) &&
(selection == pagerSettings.itemsOrderedByIndex.first && value.translation.width > 0) ||
edgeSwipeGestureDisabled.contains(.right) &&
(selection == pagerSettings.itemsOrderedByIndex.last && value.translation.width < 0))
}.updating($translation) { value, state, _ in
if selection == pagerSettings.itemsOrderedByIndex.first && value.translation.width > 0 {
let normTrans = value.translation.width / (geometryProxy.size.width + 50)
let logValue = log(1 + normTrans)
state = geometryProxy.size.width/1.5 * logValue
} else if selection == pagerSettings.itemsOrderedByIndex.last && value.translation.width < 0 {
let normTrans = -value.translation.width / (geometryProxy.size.width + 50)
let logValue = log(1 + normTrans)
state = -geometryProxy.size.width / 1.5 * logValue
} else {
state = value.translation.width
}
}.onEnded { value in
let offset = value.predictedEndTranslation.width / geometryProxy.size.width
let selectionIndex = pagerSettings.indexOf(tag: selection) ?? 0
let newPredictedIndex = (CGFloat(selectionIndex) - offset).rounded()
let newIndex = min(max(Int(newPredictedIndex), 0), pagerSettings.items.count - 1)
if newIndex > selectionIndex {
selection = pagerSettings.nextSelection(for: selection)
} else if newIndex < selectionIndex {
selection = pagerSettings.previousSelection(for: selection)
}
}
: nil)
.onAppear {
let frame = geometryProxy.frame(in: .local)
pagerSettings.width = frame.width
if let index = pagerSettings.indexOf(tag: selection) {
pagerSettings.contentOffset = -CGFloat(index) * frame.width
}
}
.onChange(of: pagerSettings.itemsOrderedByIndex) { _ in
pagerSettings.contentOffset = -(CGFloat(pagerSettings.indexOf(tag: selection) ?? 0) * geometryProxy.size.width)
}
.onChange(of: geometryProxy.frame(in: .local)) { geometry in
pagerSettings.width = geometry.width
if let index = pagerSettings.indexOf(tag: selection) {
pagerSettings.contentOffset = -(CGFloat(index)) * geometry.width
}
}
.onChange(of: selection) { newSelection in
pagerSettings.contentOffset = -(CGFloat(pagerSettings.indexOf(tag: newSelection) ?? 0) * geometryProxy.size.width)
swipeOn = true
}
.onChange(of: translation) { _ in
pagerSettings.contentOffset = translation - (CGFloat(pagerSettings.indexOf(tag: selection) ?? 0) * geometryProxy.size.width)
swipeOn = true
}
}
.modifier(NavBarModifier(selection: $selection))
.environmentObject(pagerSettings)
.clipped()
}
/*
@MainActor public var body: some View {
GeometryReader { geometryProxy in
LazyHStack(spacing: 0) {
@ -160,5 +237,14 @@ private struct WrapperPagerTabStripView<SelectionType, Content>: View where Sele
.environmentObject(pagerSettings)
.clipped()
}
*/
}
fileprivate struct ScrollViewOffsetPreferenceKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}