Created
June 20, 2020 15:59
-
-
Save shaundon/47b818b1c7c7f78cba1aa9b877b1f958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PageSlide.swift | |
// personal-best | |
// | |
// Created by Shaun Donnelly on 20/06/2020. | |
// Copyright © 2020 Shaun Donnelly. All rights reserved. | |
// | |
import SwiftUI | |
struct PageSlideControl: View { | |
struct PageSlideButtonStyle: ViewModifier { | |
func body(content: Content) -> some View { | |
return content | |
.padding() | |
.foregroundColor(.white) | |
.background(Color.blue) | |
} | |
} | |
@Binding var selectedPageIndex: Int | |
let titleOne: String | |
let titleTwo: String | |
var body: some View { | |
HStack(spacing: 0) { | |
Button(action: { | |
withAnimation { | |
self.selectedPageIndex = 0 | |
} | |
}) { | |
Text(titleOne).bold() | |
}.modifier(PageSlideButtonStyle()) | |
Button(action: { | |
withAnimation { | |
self.selectedPageIndex = 1 | |
} | |
}) { | |
Text(titleTwo).bold() | |
}.modifier(PageSlideButtonStyle()) | |
} | |
.cornerRadius(25.0) | |
.shadow(radius: 5.0) | |
} | |
} | |
struct PageSlide<FirstPage: View, SecondPage: View>: View { | |
let firstPage: FirstPage | |
let firstPageTitle: String | |
let secondPage: SecondPage | |
let secondPageTitle: String | |
@State var selectedPageIndex = 0 | |
init( | |
@ViewBuilder firstPage: () -> FirstPage, | |
firstPageTitle: String, | |
@ViewBuilder secondPage: () -> SecondPage, | |
secondPageTitle: String | |
) { | |
self.firstPage = firstPage() | |
self.firstPageTitle = firstPageTitle | |
self.secondPage = secondPage() | |
self.secondPageTitle = secondPageTitle | |
} | |
var body: some View { | |
ZStack(alignment: .bottom) { | |
if selectedPageIndex == 0 { | |
firstPage.transition(AnyTransition.slide.animation(.easeInOut)) | |
} else { | |
secondPage.transition(AnyTransition.slide.animation(.easeInOut)) | |
} | |
PageSlideControl( | |
selectedPageIndex: $selectedPageIndex, | |
titleOne: firstPageTitle, | |
titleTwo: secondPageTitle | |
) | |
} | |
} | |
} | |
struct PageSlide_Previews: PreviewProvider { | |
static var previews: some View { | |
PageSlide( | |
firstPage: { | |
List { | |
Text("One") | |
Text("Two") | |
Text("Three") | |
} | |
}, | |
firstPageTitle: "Workouts", | |
secondPage: { | |
List { | |
Text("Four") | |
Text("Five") | |
Text("Six") | |
} | |
}, | |
secondPageTitle: "Stats" | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is unfinished.