Skip to content

Commit

Permalink
fix button modifiler
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed Apr 26, 2020
1 parent fd46e53 commit 18553f7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions DemoApp/DemoApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func sceneDidEnterBackground(_ scene: UIScene) {
}
}

1 change: 1 addition & 0 deletions DemoApp/DemoApp/Views/NeumorphismButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct NeumorphismButtonView: View {
height: 300,
imageWidth: 100,
imageHeight: 100,
color: Color(hex: "2C292C"),
shadowRadius: 8
) {
print(#function)
Expand Down
1 change: 1 addition & 0 deletions DemoApp/DemoApp/Views/NeumorphismSliderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ struct NeumorphismSliderView: View {
struct NeumorphismSliderView_Previews: PreviewProvider {
static var previews: some View {
NeumorphismSliderView()
.previewLayout(.sizeThatFits)
}
}
8 changes: 4 additions & 4 deletions DemoApp/DemoApp/Views/SimpleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ struct SimpleView: View {

var body: some View {
ZStack {
Color(hex: "C1D2EB").edgesIgnoringSafeArea(.all)
neumorphism.color.edgesIgnoringSafeArea(.all)
Circle()
.fill(Color(hex: "C1D2EB"))
.fill(neumorphism.color)
.frame(width: 200, height: 200)
.shadow(color: Color(hex: "C1D2EB").darkerColor(), radius: 16, x: 8, y: 8)
.shadow(color: Color(hex: "C1D2EB").lighterColor(), radius: 16, x: -8, y: -8)
.neumorphismShadow()
}
}
}
Expand All @@ -28,3 +27,4 @@ struct SimpleView_Previews: PreviewProvider {
.previewLayout(.sizeThatFits)
}
}

11 changes: 9 additions & 2 deletions Sources/CustomViews/NeumorphismButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public struct NeumorphismButton: View {
private var height: CGFloat?
private var imageWidth: CGFloat?
private var imageHeight: CGFloat?
private var color: Color?
private var shadowRadius: CGFloat
private var handler: (() -> Void)?

Expand All @@ -24,6 +25,7 @@ public struct NeumorphismButton: View {
height: CGFloat? = nil,
imageWidth: CGFloat? = nil,
imageHeight: CGFloat? = nil,
color: Color? = nil,
shadowRadius: CGFloat = 8,
handler: (() -> Void)? = nil
) {
Expand All @@ -34,6 +36,7 @@ public struct NeumorphismButton: View {
self.height = height
self.imageWidth = imageWidth
self.imageHeight = imageHeight
self.color = color
self.shadowRadius = shadowRadius
self.handler = handler
}
Expand All @@ -51,9 +54,13 @@ public struct NeumorphismButton: View {
.background(
Rectangle()
.clipShape(self.shapeType.anyShape)
.foregroundColor(self.neumorphism.color)
.foregroundColor(self.color ?? self.neumorphism.color)
.frame(width: self.width, height: self.height)
.modifier(self.isSelected ? NeumorphismShadowModifier(radius: self.shadowRadius, isAnimation: isHeighlight ) : NeumorphismShadowModifier(radius: self.shadowRadius, isAnimation: isHeighlight))
.modifier(
self.isSelected
? NeumorphismShadowModifier(baseColor: self.color, radius: self.shadowRadius, isAnimation: isHeighlight)
: NeumorphismShadowModifier(baseColor: self.color, radius: self.shadowRadius, isAnimation: isHeighlight)
)
)
.padding()
.animation(Animation.spring(response: 0.3, dampingFraction: 0.7, blendDuration: 1))
Expand Down
6 changes: 2 additions & 4 deletions Sources/Extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import SwiftUI
@available(iOS 13.0, *)
public extension View {
func neumorphismShadow(
darkShadowColor: Color? = nil,
lightShadowColor: Color? = nil,
baseColor: Color? = nil,
radius: CGFloat = 16,
x: CGFloat = 8,
y: CGFloat = 8,
isAnimation: Bool = false
) -> some View {
self.modifier(NeumorphismShadowModifier(
darkShadowColor: darkShadowColor,
lightShadowColor: lightShadowColor,
baseColor: baseColor,
radius: radius,
x: x,
y: y,
Expand Down
13 changes: 5 additions & 8 deletions Sources/Modifier/NeumorphismShadowModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ import SwiftUI
public struct NeumorphismShadowModifier: ViewModifier {
@EnvironmentObject var neumorphism: NeumorphismManager

private let darkShadowColor: Color?
private let lightShadowColor: Color?
private let baseColor: Color?

private let radius: CGFloat
private let x: CGFloat
private let y: CGFloat

public init(
darkShadowColor: Color? = nil,
lightShadowColor: Color? = nil,
baseColor: Color? = nil,
radius: CGFloat = 16,
x: CGFloat = 8,
y: CGFloat = 8,
isAnimation: Bool = false
) {
self.darkShadowColor = darkShadowColor
self.lightShadowColor = lightShadowColor
self.baseColor = baseColor
if isAnimation {
self.x = -x / 2
self.y = -y / 2
Expand All @@ -35,12 +32,12 @@ public struct NeumorphismShadowModifier: ViewModifier {
public func body(content: Content) -> some View {
content
.shadow(
color: darkShadowColor ?? neumorphism.color.darkerColor(),
color: baseColor?.darkerColor() ?? neumorphism.color.darkerColor(),
radius: radius,
x: x, y: y
)
.shadow(
color: lightShadowColor ?? neumorphism.color.lighterColor(),
color: baseColor?.lighterColor() ?? neumorphism.color.lighterColor(),
radius: radius,
x: -x, y: -y
)
Expand Down

0 comments on commit 18553f7

Please sign in to comment.