Skip to content

Commit

Permalink
prizes views
Browse files Browse the repository at this point in the history
  • Loading branch information
moyoteg committed May 28, 2023
1 parent 9aee50f commit 4d283be
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 65 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import PackageDescription
let package = Package(
name: "SwiftUIComponents",
platforms: [
.iOS("15"),
.macOS("13.1"),
.tvOS("15"),
.iOS("16"),
.macOS("14"),
.tvOS("16"),
.watchOS(.v7),
],
products: [
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftUIComponents/Extensions/ClearButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public struct ClearButton: ViewModifier {
self.text = ""
}
}) {
AutoImage("delete.left")
.renderingMode(.template)
Image(systemName: "delete.left")
.foregroundColor(foregroundColor)
}
.opacity(text.isEmpty ? 0:1)
Expand Down
13 changes: 7 additions & 6 deletions Sources/SwiftUIComponents/GridLastCellTakesFullWidth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,37 @@ import SwiftUI
public struct GridLastCellTakesFullWidth<Data: RandomAccessCollection, Content: View>: View where Data.Element: Identifiable & Hashable {
let data: Data
let columns: [GridItem]
let spacing: CGFloat
let content: (Data.Element) -> Content

public var body: some View {
ScrollView {
if data.count % 2 != 0 {
LazyVGrid(columns: columns, alignment: .center, spacing: 10) {
LazyVGrid(columns: columns, alignment: .center, spacing: spacing) {
ForEach(data.dropLast().array ?? [], id: \.self) { item in
content(item)
}
}
LazyVGrid(columns: [GridItem(.flexible())], alignment: .center, spacing: 10) {
LazyVGrid(columns: [GridItem(.flexible())], alignment: .center, spacing: spacing) {
ForEach(data.suffix(1).array ?? [], id: \.self) { item in
content(item)
.frame(maxWidth: .infinity)
}
}
} else {
LazyVGrid(columns: columns, alignment: .center, spacing: 10) {
LazyVGrid(columns: columns, alignment: .center, spacing: spacing) {
ForEach(data.array ?? [], id: \.self) { item in
content(item)
}
}
}
}
.padding()
}

public init(data: Data, columns: [GridItem], @ViewBuilder content: @escaping (Data.Element) -> Content) {
public init(data: Data, columns: [GridItem], spacing: CGFloat, @ViewBuilder content: @escaping (Data.Element) -> Content) {
self.data = data
self.columns = columns
self.spacing = spacing
self.content = content
}
}
Expand All @@ -61,7 +62,7 @@ public struct Example_GridLastCellTakesFullWidth: View {
let columns = Array(repeating: GridItem(.flexible()), count: 2)

public var body: some View {
GridLastCellTakesFullWidth(data: data, columns: columns) { item in
GridLastCellTakesFullWidth(data: data, columns: columns, spacing: 8) { item in
Text("\(item.number)")
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.blue)
Expand Down
30 changes: 16 additions & 14 deletions Sources/SwiftUIComponents/PageControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import SwiftUI
public struct PageControl: View {

@Binding var currentpage: Int
@Binding var pageCount: Int
var pageCount: Int
@Binding var isSelecting: Bool

var pageControlColor: Color
var selectionColor: Color
var blurRadius: CGFloat

// visuals
let pageControlSize: CGFloat = 8

var buttonShape: AnyShape
var buttonColor: (Int) -> Color

var pageControlSize: CGSize = .init(width: 8, height: 8)

public var body: some View {

Expand All @@ -36,17 +38,17 @@ public struct PageControl: View {
}
}) {
if (currentpage == row) {
Circle()
buttonShape
.fill(selectionColor)
.frame(width: pageControlSize,
height: pageControlSize,
alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
.frame(width: pageControlSize.width,
height: pageControlSize.height,
alignment: .center)
} else {
Circle()
.fill(pageControlColor.opacity(0.5))
.frame(width: pageControlSize,
height: pageControlSize,
alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
buttonShape
.fill(buttonColor(row).opacity(0.5))
.frame(width: pageControlSize.width,
height: pageControlSize.height,
alignment: .center)
}
}
.transition(.scale(scale: 2.0))
Expand Down
95 changes: 55 additions & 40 deletions Sources/SwiftUIComponents/PagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import AVFoundation

#if !os(tvOS)

public struct PagesView<Content: View>: View {

public struct PagesView<Data, Content: View>: View where Data: RandomAccessCollection, Data.Element: Identifiable {
public enum SelectionDirection {
case horizontal
// TODO: implement
case vertical
}

@Binding var pageCount: Int
var data: Data
@Binding var currentIndex: Int
@Binding var isSelecting: Bool
@GestureState private var translation: CGFloat = 0
Expand All @@ -29,17 +29,23 @@ public struct PagesView<Content: View>: View {

var selectionDirection: SelectionDirection
let selectionTolerance: CGFloat = 0.1
let content: Content
let content: (Data.Element) -> Content
let animationsDuration = 0.3

var buttonShape: AnyShape
var buttonColor: (Int) -> Color
var pageControlSize: CGSize

public var body: some View {

GeometryReader { geometry in

ZStack {
HStack(spacing: 8) {
self.content
.frame(width: geometry.size.width)
ForEach(data) { item in
self.content(item)
.frame(width: geometry.size.width)
}
}
.frame(width: geometry.size.width, alignment: .leading)
.offset(x: -CGFloat(self.currentIndex) * geometry.size.width)
Expand All @@ -50,7 +56,7 @@ public struct PagesView<Content: View>: View {
.onChanged({ (value) in

// if there is only one page don't enable interaction
if pageCount <= 1 { return }
if data.count <= 1 { return }

// check if translating in "selection direction"
switch selectionDirection {
Expand All @@ -73,75 +79,84 @@ public struct PagesView<Content: View>: View {
.updating(self.$translation) { value, state, _ in

// if there is only one page don't enable interaction
if pageCount <= 1 { return }
if data.count <= 1 { return }

state = value.translation.width

}.onEnded { value in
let offset = value.translation.width / geometry.size.width
let newIndex = Int((CGFloat(self.currentIndex) - offset).rounded())
withAnimation {
self.currentIndex = min(max(newIndex, 0), self.pageCount - 1)
self.currentIndex = min(max(newIndex, 0), data.count - 1)
DispatchQueue.main
.asyncAfter(deadline: .now() + animationsDuration) {
update(selectingState: false)
}
update(selectingState: false)
}
}

}
)
)

VStack {
Spacer()
PageControl(currentpage: $currentIndex,
pageCount: $pageCount,
isSelecting: $isSelecting,
pageControlColor: pageControlColor,
selectionColor: selectionColor,
blurRadius: blurRadius)
.scaleEffect(
CGSize(width: isSelecting ? 2.0:1.0,
height: isSelecting ? 2.0:1.0)
)
PageControl(
currentpage: $currentIndex,
pageCount: data.count,
isSelecting: $isSelecting,
pageControlColor: pageControlColor,
selectionColor: selectionColor,
blurRadius: blurRadius,
buttonShape: buttonShape,
buttonColor: buttonColor,
pageControlSize: pageControlSize
)
.scaleEffect(
CGSize(width: isSelecting ? 2.0:1.0,
height: isSelecting ? 2.0:1.0)
)
}
.padding()
}
}
}

public init(pageCount: Binding<Int>,
currentIndex: Binding<Int>,
isSelecting: Binding<Bool>,
selectionDirection: SelectionDirection = .horizontal,
pageControlColor: Color = .white,
selectionColor: Color = .blue,
blurRadius: CGFloat = 20.0,
@ViewBuilder content: () -> Content) {
self._pageCount = pageCount
public init(data: Data,
currentIndex: Binding<Int>,
isSelecting: Binding<Bool>,
selectionDirection: SelectionDirection = .horizontal,
pageControlColor: Color = .white,
selectionColor: Color = .blue,
blurRadius: CGFloat = 20.0,
buttonShape: AnyShape,
buttonColor: @escaping (Int) -> Color,
pageControlSize: CGSize,
@ViewBuilder content: @escaping (Data.Element) -> Content
) {
self.data = data
self._currentIndex = currentIndex
self._isSelecting = isSelecting
self.selectionDirection = selectionDirection
self.content = content()
self.content = content
self.pageControlColor = pageControlColor
self.selectionColor = selectionColor
self.blurRadius = blurRadius
self.buttonShape = buttonShape
self.buttonColor = buttonColor
self.pageControlSize = pageControlSize
}

private func update(selectingState: Bool) {
if selectingState != isSelecting {
isSelecting = selectingState
#if !os(watchOS)
UIImpactFeedbackGenerator().impactOccurred(intensity: .infinity)
AudioServicesPlaySystemSound(UInt32(1104))
#endif
}
}

private func percentageOf(width: CGFloat,
startLocation: CGFloat,
newLocation: CGFloat) -> CGFloat {
startLocation: CGFloat,
newLocation: CGFloat) -> CGFloat {
let percentage = abs(startLocation - newLocation) / width
return percentage
}
}

#endif

0 comments on commit 4d283be

Please sign in to comment.