-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from juandahurt/tab
feat: add tab view
- Loading branch information
Showing
13 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// TabExample.swift | ||
// PuraceDemo | ||
// | ||
// Created by Juan Hurtado on 1/06/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import MapKit | ||
import Purace | ||
|
||
struct IdentifiablePlace: Identifiable { | ||
let id: UUID | ||
let location: CLLocationCoordinate2D | ||
init(id: UUID = UUID(), lat: Double, long: Double) { | ||
self.id = id | ||
self.location = CLLocationCoordinate2D( | ||
latitude: lat, | ||
longitude: long) | ||
} | ||
} | ||
|
||
struct TabExample: View { | ||
let place = IdentifiablePlace(lat: 2.443881, long: -76.605059) | ||
@State private var region = MKCoordinateRegion( | ||
center: CLLocationCoordinate2D(latitude: 2.443881, | ||
longitude: -76.605059), | ||
latitudinalMeters: 750, | ||
longitudinalMeters: 750 | ||
) | ||
|
||
var body: some View { | ||
PuraceTabView(titles: ["Acerca de", "Imágenes"]) { index in | ||
Group { | ||
switch index { | ||
case 0: | ||
VStack { | ||
Map(coordinateRegion: $region, annotationItems: [place]) { item in | ||
MapMarker(coordinate: place.location, tint: .black) | ||
}.frame(height: UIScreen.main.bounds.height * 0.5) | ||
Spacer(minLength: 0) | ||
} | ||
default: | ||
ScrollView { | ||
PuraceVerticalGridView(columns: 2, spacing: 1) { | ||
PuraceImageView(url: URL(string: "https://www.biografiasyvidas.com/biografia/c/fotos/caldas_francisco_jose_2.jpg")) | ||
.frame(height: 200) | ||
PuraceImageView(url: URL(string: "https://www.biografiasyvidas.com/biografia/c/fotos/caldas_francisco_jose_2.jpg")) | ||
.frame(height: 200) | ||
PuraceImageView(url: URL(string: "https://www.biografiasyvidas.com/biografia/c/fotos/caldas_francisco_jose_2.jpg")) | ||
.frame(height: 200) | ||
PuraceImageView(url: URL(string: "https://www.biografiasyvidas.com/biografia/c/fotos/caldas_francisco_jose_2.jpg")) | ||
.frame(height: 200) | ||
} | ||
} | ||
} | ||
}.frame(height: UIScreen.main.bounds.height * 0.7) | ||
}.onAppear { | ||
MKMapView.appearance().mapType = .mutedStandard | ||
} | ||
Spacer() | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// PuraceTabItem.swift | ||
// | ||
// | ||
// Created by Juan Hurtado on 1/06/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct PuraceTabItem<T: View> { | ||
let title: String | ||
let content: () -> T | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// PuraceTabView.swift | ||
// | ||
// | ||
// Created by Juan Hurtado on 1/06/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
public struct PuraceTabView<T: View>: View { | ||
let titles: [String] | ||
@State var selectedIndex = 0 | ||
@State var indicatorOffset: CGFloat = .zero | ||
var viewForIndex: (Int) -> T | ||
|
||
public init(titles: [String], viewForIndex: @escaping (Int) -> T) { | ||
self.titles = titles | ||
self.viewForIndex = viewForIndex | ||
} | ||
|
||
func headers(in size: CGSize) -> some View { | ||
HStack(spacing: 0) { | ||
ForEach(0..<titles.count) { index in | ||
PuraceTextView(titles[index], fontSize: 12, textColor: selectedIndex == index ? PuraceStyle.Color.N1 : PuraceStyle.Color.N4, weight: selectedIndex == index ? .medium : .regular) | ||
.frame(width: size.width / CGFloat(titles.count)) | ||
.onTapGesture { | ||
selectedIndex = index | ||
updateIndicatorOffset(size: size) | ||
} | ||
} | ||
}.frame(height: 50) | ||
} | ||
|
||
func indicator(in size: CGSize) -> some View { | ||
ZStack { | ||
Color.black | ||
.frame(height: 1) | ||
.opacity(0.1) | ||
HStack { | ||
PuraceStyle.Color.B2 | ||
.frame(width: size.width / CGFloat(titles.count), height: 3) | ||
Spacer(minLength: 0) | ||
}.offset(x: indicatorOffset, y: 0) | ||
} | ||
} | ||
|
||
func updateIndicatorOffset(size: CGSize) { | ||
withAnimation(.linear(duration: 0.2)) { | ||
indicatorOffset = CGFloat(selectedIndex) * size.width / CGFloat(titles.count) | ||
} | ||
} | ||
|
||
public var body: some View { | ||
GeometryReader { reader in | ||
VStack(spacing: 0) { | ||
headers(in: reader.size) | ||
indicator(in: reader.size) | ||
ZStack { | ||
ForEach(0..<titles.count) { index in | ||
viewForIndex(index) | ||
.opacity(index == selectedIndex ? 1 : 0) | ||
} | ||
}.clipped() | ||
.contentShape(Rectangle()) | ||
} | ||
} | ||
} | ||
} |