Skip to content

Commit

Permalink
Custom NeumorphismButton
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed Mar 10, 2020
1 parent ae96bdf commit 00986ac
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 47 deletions.
4 changes: 0 additions & 4 deletions DemoApp/DemoApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
DA1A6B74240B81A6001EA5EB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DA1A6B73240B81A6001EA5EB /* Assets.xcassets */; };
DA1A6B77240B81A6001EA5EB /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DA1A6B76240B81A6001EA5EB /* Preview Assets.xcassets */; };
DA1A6B7A240B81A6001EA5EB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DA1A6B78240B81A6001EA5EB /* LaunchScreen.storyboard */; };
DA5AFAC7241517190098E0BB /* NeumorphismRoundedRectangleButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5AFAC6241517190098E0BB /* NeumorphismRoundedRectangleButton.swift */; };
DA74B267240B820400AD9A06 /* NeumorphismUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA74B266240B820400AD9A06 /* NeumorphismUI.framework */; };
DA74B268240B820400AD9A06 /* NeumorphismUI.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DA74B266240B820400AD9A06 /* NeumorphismUI.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -41,7 +40,6 @@
DA1A6B76240B81A6001EA5EB /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
DA1A6B79240B81A6001EA5EB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
DA1A6B7B240B81A6001EA5EB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DA5AFAC6241517190098E0BB /* NeumorphismRoundedRectangleButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismRoundedRectangleButton.swift; sourceTree = "<group>"; };
DA74B266240B820400AD9A06 /* NeumorphismUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = NeumorphismUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -80,7 +78,6 @@
DA1A6B6D240B81A4001EA5EB /* AppDelegate.swift */,
DA1A6B6F240B81A4001EA5EB /* SceneDelegate.swift */,
DA1A6B71240B81A4001EA5EB /* ContentView.swift */,
DA5AFAC6241517190098E0BB /* NeumorphismRoundedRectangleButton.swift */,
DA1A6B73240B81A6001EA5EB /* Assets.xcassets */,
DA1A6B78240B81A6001EA5EB /* LaunchScreen.storyboard */,
DA1A6B7B240B81A6001EA5EB /* Info.plist */,
Expand Down Expand Up @@ -177,7 +174,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA5AFAC7241517190098E0BB /* NeumorphismRoundedRectangleButton.swift in Sources */,
DA1A6B6E240B81A4001EA5EB /* AppDelegate.swift in Sources */,
DA1A6B70240B81A4001EA5EB /* SceneDelegate.swift in Sources */,
DA1A6B72240B81A4001EA5EB /* ContentView.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions DemoApp/DemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ struct ContentView: View {
.font(.title)
.foregroundColor(self.neumorphism.color.darkerColor())

NeumorphismButton() {
NeumorphismButton(shapeType: .circle) {
self.neumorphism.changeMode()
}

NeumorphismButton(normalImage: Image(systemName: "star"), selectedImage: Image(systemName: "star.fill"))
NeumorphismButton(shapeType: .roundedRectangle(cornerRadius: 20), normalImage: Image(systemName: "star"), selectedImage: Image(systemName: "star.fill"))

Circle()
.fill(self.neumorphism.color)
Expand Down
35 changes: 0 additions & 35 deletions DemoApp/DemoApp/NeumorphismRoundedRectangleButton.swift

This file was deleted.

8 changes: 8 additions & 0 deletions NeumorphismUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

/* Begin PBXBuildFile section */
DA5AFAC9241691210098E0BB /* NeumorphismButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */; };
DA5AFACD2418332A0098E0BB /* ShapeType.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5AFACC2418332A0098E0BB /* ShapeType.swift */; };
DA5AFACF241833430098E0BB /* AnyShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5AFACE241833430098E0BB /* AnyShape.swift */; };
DA90AF652412D01900E759A9 /* NeumorphismManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA90AF642412D01900E759A9 /* NeumorphismManagerTests.swift */; };
DA90AF682412D0EE00E759A9 /* NeumorphismManagerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA90AF672412D0EE00E759A9 /* NeumorphismManagerMock.swift */; };
DA954C782411D9740052B8FF /* NeumorphismManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA954C772411D9740052B8FF /* NeumorphismManager.swift */; };
Expand Down Expand Up @@ -55,6 +57,8 @@
/* Begin PBXFileReference section */
DA1A6B5F240B76D9001EA5EB /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismButton.swift; sourceTree = "<group>"; };
DA5AFACC2418332A0098E0BB /* ShapeType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapeType.swift; sourceTree = "<group>"; };
DA5AFACE241833430098E0BB /* AnyShape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyShape.swift; sourceTree = "<group>"; };
DA90AF642412D01900E759A9 /* NeumorphismManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismManagerTests.swift; sourceTree = "<group>"; };
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>"; };
Expand Down Expand Up @@ -162,6 +166,8 @@
DA954C772411D9740052B8FF /* NeumorphismManager.swift */,
OBJ_9 /* ColorTransformer.swift */,
OBJ_13 /* NeumorphismShadowModifier.swift */,
DA5AFACE241833430098E0BB /* AnyShape.swift */,
DA5AFACC2418332A0098E0BB /* ShapeType.swift */,
DA987319240BDE580083EB5E /* CustomViews */,
OBJ_10 /* Extensions */,
);
Expand Down Expand Up @@ -258,8 +264,10 @@
DA90AF682412D0EE00E759A9 /* NeumorphismManagerMock.swift in Sources */,
DA954C782411D9740052B8FF /* NeumorphismManager.swift in Sources */,
DA5AFAC9241691210098E0BB /* NeumorphismButton.swift in Sources */,
DA5AFACD2418332A0098E0BB /* ShapeType.swift in Sources */,
OBJ_32 /* ViewExtension.swift in Sources */,
OBJ_33 /* NeumorphismShadowModifier.swift in Sources */,
DA5AFACF241833430098E0BB /* AnyShape.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
17 changes: 17 additions & 0 deletions Sources/AnyShape.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SwiftUI

@available(iOS 13.0.0, *)
public struct AnyShape: Shape {
public init<S: Shape>(_ wrapped: S) {
_path = { rect in
let path = wrapped.path(in: rect)
return path
}
}

public func path(in rect: CGRect) -> Path {
return _path(rect)
}

private let _path: (CGRect) -> Path
}
28 changes: 24 additions & 4 deletions Sources/CustomViews/NeumorphismButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import SwiftUI
public struct NeumorphismButton: View {
@EnvironmentObject var neumorphism: NeumorphismManager
@State var isSelected = false


private var shapeType: ShapeType
private var normalImage: Image
private var selectedImage: Image
private var width: CGFloat
private var height: CGFloat
private var imageWidth: CGFloat
private var imageHeight: CGFloat
private var handler: (() -> Void)?

public init
(
shapeType: ShapeType = .circle,
normalImage: Image = Image(systemName: "heart"),
selectedImage: Image = Image(systemName: "heart.fill"),
width: CGFloat = 100,
Expand All @@ -23,6 +25,7 @@ public struct NeumorphismButton: View {
imageHeight: CGFloat = 60,
handler: (() -> Void)? = nil
) {
self.shapeType = shapeType
self.normalImage = normalImage
self.selectedImage = selectedImage
self.width = width
Expand All @@ -43,8 +46,9 @@ public struct NeumorphismButton: View {
.frame(width:self.imageWidth, height: self.imageWidth)
.foregroundColor(self.neumorphism.color.darkerColor())
.background(
Circle()
.fill(self.neumorphism.color)
Rectangle()
.clipShape(self.getAnyShape(type: self.shapeType))
.foregroundColor(self.neumorphism.color)
.frame(width: self.width, height: self.height)
.modifier(NeumorphismShadowModifier(isAnimation: isHeighlight))
)
Expand All @@ -53,4 +57,20 @@ public struct NeumorphismButton: View {
}
.padding()
}

public func getAnyShape(type: ShapeType) -> AnyShape {
switch type {
case .rectangle:
return AnyShape(Rectangle())
case .roundedRectangle(let cornerRadius):
return AnyShape(RoundedRectangle(cornerRadius: cornerRadius))
case .capsule:
return AnyShape(Capsule(style: .circular))
case .ellipse:
return AnyShape(Ellipse())
case .circle:
return AnyShape(Circle())
}
}
}

4 changes: 2 additions & 2 deletions Sources/Extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

@available(iOS 13.0, *)
public extension View {
func neumorphismShadow() -> some View {
self.modifier(NeumorphismShadowModifier())
func neumorphismShadow(isAnimation: Bool = false) -> some View {
self.modifier(NeumorphismShadowModifier(isAnimation: isAnimation))
}
}
9 changes: 9 additions & 0 deletions Sources/ShapeType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SwiftUI

public enum ShapeType {
case rectangle
case roundedRectangle(cornerRadius: CGFloat)
case capsule
case ellipse
case circle
}

0 comments on commit 00986ac

Please sign in to comment.