Skip to content

Commit

Permalink
Add an item, 229, Update AxisSheet library.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasudev committed Feb 20, 2022
1 parent 2e831af commit fedd6cc
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"repositoryURL": "https://github.com/jasudev/AxisSheet.git",
"state": {
"branch": "main",
"revision": "f9b0e5bcf5cd5375173d4cb5b64801713cbd8ea6",
"revision": "251961e7d7d240538d77d3fbf5045dd7ade0b641",
"version": null
}
},
Expand Down
37 changes: 34 additions & 3 deletions Sources/FabulaItemsProvider/Common/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
//

import SwiftUI
import AxisSheet

public let FABULA_APP_URL: String = "https://apps.apple.com/app/id1591155142"
public let FABULA_APP_INSTAGRAM_URL = "https://www.instagram.com/dev.fabula"
public let FABULA_GITHUB_URL = "https://github.com/jasudev/FabulaItemsProvider"

public var isPad: Bool {
#if os(iOS)
#if os(iOS)
return UIDevice.current.userInterfaceIdiom != .phone
#else
#else
return false
#endif
#endif
}

#if os(iOS)
Expand All @@ -29,3 +30,33 @@ public func vibration(_ style: UIImpactFeedbackGenerator.FeedbackStyle = .soft)
#else
public func vibration() {}
#endif

public extension View {
func adaptiveAxisSheet(isPresented: Binding<Bool>, bottomSize: CGFloat = 110, trailingSize: CGFloat = 260) -> some View {
GeometryReader { proxy in
if proxy.size.width < proxy.size.height {
let constants = ASConstant(axisMode: .bottom, size: bottomSize,
header: ASHeaderConstant(backgroundColor: .fabulaBack0.opacity(0.9)),
background: ASBackgroundConstant(disabled: true))
AxisSheet(isPresented: isPresented, constants: constants) {
VStack {
self
Spacer()
}
.background(Color.fabulaBack2.opacity(0.8))
}
}else {
let constants = ASConstant(axisMode: .trailing, size: trailingSize,
header: ASHeaderConstant(backgroundColor: .fabulaBack0.opacity(0.9)),
background: ASBackgroundConstant(disabled: true))
AxisSheet(isPresented: isPresented, constants: constants) {
VStack {
self
Spacer()
}
.background(Color.fabulaBack2.opacity(0.8))
}
}
}
}
}
22 changes: 11 additions & 11 deletions Sources/FabulaItemsProvider/Items/P228_AxisSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ public struct P228_AxisSheet: View {
}

private func getHeaderAngle() -> Angle {
switch constants.axisType {
switch constants.axisMode {
case .top, .bottom: return Angle(degrees: 0)
case .leading: return Angle(degrees: 90)
case .trailing: return Angle(degrees: -90)
}
}

private func getOffset() -> CGSize {
guard constants.presentationType == .minimize else {
guard constants.presentationMode == .minimize else {
return .zero
}
switch constants.axisType {
switch constants.axisMode {
case .top: return CGSize(width: 0, height: constants.header.size)
case .bottom: return CGSize(width: 0, height: -constants.header.size)
case .leading: return CGSize(width: constants.header.size, height: 0)
Expand Down Expand Up @@ -169,15 +169,15 @@ struct SheetControls: View {
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
.labelsHidden()
VStack(alignment: .trailing) {
Picker("", selection: $constants.axisType) {
Image(systemName: "rectangle.bottomthird.inset.filled").tag(ASAxisType.bottom)
Image(systemName: "rectangle.rightthird.inset.filled").tag(ASAxisType.trailing)
Image(systemName: "rectangle.leadingthird.inset.filled").tag(ASAxisType.leading)
Image(systemName: "rectangle.topthird.inset.filled").tag(ASAxisType.top)
Picker("", selection: $constants.axisMode) {
Image(systemName: "rectangle.bottomthird.inset.filled").tag(ASAxisMode.bottom)
Image(systemName: "rectangle.rightthird.inset.filled").tag(ASAxisMode.trailing)
Image(systemName: "rectangle.leadingthird.inset.filled").tag(ASAxisMode.leading)
Image(systemName: "rectangle.topthird.inset.filled").tag(ASAxisMode.top)
}
Picker("", selection: $constants.presentationType) {
Text("Minimize").tag(ASPresentationType.minimize)
Text("Hide").tag(ASPresentationType.hide)
Picker("", selection: $constants.presentationMode) {
Text("Minimize").tag(ASPresentationMode.minimize)
Text("Hide").tag(ASPresentationMode.hide)
}
Picker("", selection: $isCustomHeader) {
Text("Normal").tag(false)
Expand Down
107 changes: 107 additions & 0 deletions Sources/FabulaItemsProvider/Items/P229_StarShape.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// P229_StarShape.swift
//
//
// Created by jasu on 2022/02/19.
// Copyright (c) 2022 jasu All rights reserved.
//

import SwiftUI
import AxisSheet

public struct P229_StarShape: View {

@State private var count: CGFloat = 5
@State private var ratio: CGFloat = 1
@State private var isPresented: Bool = true

public init() {}
public var body: some View {
ZStack {
VStack {
GeometryReader { proxy in
let min = min(proxy.size.width, proxy.size.height)
ZStack {
Color.clear
Star(count: round(count), innerRatio: ratio)
.fill(Color.accentColor)
.frame(width: min, height: min)
}
}
}
.padding()
.animation(.easeInOut, value: count)
.animation(.easeInOut, value: ratio)

VStack {
HStack {
Text("Count : ")
Slider(value: $count, in: 1...10)
Text("\(count, specifier: "%.2f")")
}
HStack {
Text("InnerRatio : ")
Slider(value: $ratio, in: 0...2)
Text("\(ratio, specifier: "%.2f")")
}
}
.padding()
.adaptiveAxisSheet(isPresented: $isPresented, bottomSize: 110)
}
}
}

fileprivate
struct Star: Shape {

var count: CGFloat
var innerRatio: CGFloat

var animatableData: AnimatablePair<CGFloat, CGFloat> {
get { AnimatablePair(count, innerRatio) }
set {
count = newValue.first
innerRatio = newValue.second
}
}

func path(in rect: CGRect) -> Path {
let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
let pointAngle = .pi / count

let innerPoint = CGPoint(x: center.x * innerRatio * 0.5, y: center.y * innerRatio * 0.5)
let totalPoints = Int(count * 2.0)

var currentAngle = CGFloat.pi * -0.5
var currentBottom: CGFloat = 0

var path = Path()
path.move(to: CGPoint(x: center.x * cos(currentAngle),
y: center.y * sin(currentAngle)))

let correction = count != round(count) ? 1 : 0
for corner in 0..<totalPoints + correction {
var bottom: CGFloat = 0
let sin = sin(currentAngle)
let cos = cos(currentAngle)
if (corner % 2) == 0 {
bottom = center.y * sin
path.addLine(to: CGPoint(x: center.x * cos, y: bottom))
} else {
bottom = innerPoint.y * sin
path.addLine(to: CGPoint(x: innerPoint.x * cos, y: bottom))
}
currentBottom = max(bottom, currentBottom)
currentAngle += pointAngle
}

let transform = CGAffineTransform(translationX: center.x, y: center.y + ((rect.height * 0.5 - currentBottom) * 0.5))
return path.applying(transform)
}
}

struct P229_StarShape_Previews: PreviewProvider {
static var previews: some View {
P229_StarShape()
}
}
8 changes: 8 additions & 0 deletions Sources/FabulaItemsProvider/ItemsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class ItemsProvider {

public var items: [ItemData] {
[
ItemData(id: 229, category: .uiux,
section: "Shape",
createDate: "2022-02-19",
title: "Star",
caption: "How to make a star shape.",
creator: "jasu",
tags: "AnimatablePair",
view: FAnyView(P229_StarShape())),
ItemData(id: 228, category: .uiux,
section: "Library",
createDate: "2022-02-17",
Expand Down

0 comments on commit fedd6cc

Please sign in to comment.