Skip to content

Commit

Permalink
Add NeumorphismLabelButton
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed Mar 13, 2020
1 parent f0d8781 commit efffec6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NeumorphismUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
DA90AF682412D0EE00E759A9 /* NeumorphismManagerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA90AF672412D0EE00E759A9 /* NeumorphismManagerMock.swift */; };
DA954C782411D9740052B8FF /* NeumorphismManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA954C772411D9740052B8FF /* NeumorphismManager.swift */; };
DA98731B240BDE7D0083EB5E /* HighlightableButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA98731A240BDE7D0083EB5E /* HighlightableButton.swift */; };
DAD95493241B4E0200D3F56E /* NeumorphismLabelButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD95492241B4E0200D3F56E /* NeumorphismLabelButton.swift */; };
OBJ_30 /* ColorTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* ColorTransformer.swift */; };
OBJ_31 /* ColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* ColorExtension.swift */; };
OBJ_32 /* ViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* ViewExtension.swift */; };
Expand Down Expand Up @@ -63,6 +64,7 @@
DA90AF672412D0EE00E759A9 /* NeumorphismManagerMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismManagerMock.swift; sourceTree = "<group>"; };
DA954C772411D9740052B8FF /* NeumorphismManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismManager.swift; sourceTree = "<group>"; };
DA98731A240BDE7D0083EB5E /* HighlightableButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HighlightableButton.swift; sourceTree = "<group>"; };
DAD95492241B4E0200D3F56E /* NeumorphismLabelButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismLabelButton.swift; sourceTree = "<group>"; };
"NeumorphismUI::NeumorphismUI::Product" /* NeumorphismUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = NeumorphismUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
"NeumorphismUI::NeumorphismUITests::Product" /* NeumorphismUITests.xctest */ = {isa = PBXFileReference; lastKnownFileType = file; path = NeumorphismUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_11 /* ColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -123,6 +125,7 @@
children = (
DA98731A240BDE7D0083EB5E /* HighlightableButton.swift */,
DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */,
DAD95492241B4E0200D3F56E /* NeumorphismLabelButton.swift */,
);
path = CustomViews;
sourceTree = "<group>";
Expand Down Expand Up @@ -274,6 +277,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 0;
files = (
DAD95493241B4E0200D3F56E /* NeumorphismLabelButton.swift in Sources */,
OBJ_30 /* ColorTransformer.swift in Sources */,
OBJ_31 /* ColorExtension.swift in Sources */,
DA98731B240BDE7D0083EB5E /* HighlightableButton.swift in Sources */,
Expand Down
70 changes: 70 additions & 0 deletions Sources/CustomViews/NeumorphismLabelButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import SwiftUI

@available(iOS 13.0.0, *)
struct NeumorphismLabelButton: View {
@EnvironmentObject var neumorphism: NeumorphismManager
@State var isSelected = false
private var text: String
private var font: Font?
private var color: Color?
private var width: CGFloat
private var height: CGFloat
private var cornerRadius: CGFloat
private var handler: (() -> Void)?

public init
(
text: String = "Button",
font: Font? = nil,
color: Color? = nil,
width: CGFloat = 100,
height: CGFloat = 100,
cornerRadius: CGFloat = 10,
handler: (() -> Void)? = nil
) {
self.text = text
self.font = font
self.color = color
self.width = width
self.height = height
self.cornerRadius = cornerRadius
self.handler = handler
}

public var body: some View {
HighlightableButton(action: {
self.isSelected.toggle()
self.handler?()
}) { isHeighlight in
ZStack {
RoundedRectangle(cornerRadius: self.cornerRadius)
.fill(self.neumorphism.color)
.frame(width: self.width, height: self.height)
.modifier(NeumorphismShadowModifier(isAnimation: isHeighlight))
.padding()


Text(self.text)
.font(self.font ?? .title)
.foregroundColor(
isHeighlight ? self.color?.darkerColor() ?? self.neumorphism.color.darkerColor().darkerColor() : self.color ?? self.neumorphism.color.darkerColor())
}
.animation(Animation.spring(response: 0.3, dampingFraction: 0.7, blendDuration: 1))
}
.padding()
}
}

@available(iOS 13.0.0, *)
struct NeumorphismLabelButton_Previews: PreviewProvider {
static let neumorphism = NeumorphismManager(
lightColor: Color(hex: "C1D2EB"),
darkColor: Color(hex: "2C292C")
)

static var previews: some View {
NeumorphismLabelButton()
.environmentObject(neumorphism)
.previewLayout(.sizeThatFits)
}
}

0 comments on commit efffec6

Please sign in to comment.