Skip to content

Commit

Permalink
Neu ConcaveModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed Apr 29, 2020
1 parent 0e234c0 commit f9f409b
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 58 deletions.
8 changes: 4 additions & 4 deletions DemoApp/DemoApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
DA1A6B7A240B81A6001EA5EB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DA1A6B78240B81A6001EA5EB /* LaunchScreen.storyboard */; };
DA5AFAD32418367C0098E0BB /* SimpleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5AFAD22418367C0098E0BB /* SimpleView.swift */; };
DA5D60CC2422727E0021B0B7 /* BaseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5D60CB2422727E0021B0B7 /* BaseView.swift */; };
DA5D60CF2422767E0021B0B7 /* DentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5D60CE2422767E0021B0B7 /* DentView.swift */; };
DA5D60CF2422767E0021B0B7 /* Concave.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5D60CE2422767E0021B0B7 /* Concave.swift */; };
DA5D60D1242277C50021B0B7 /* NeumorphismButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5D60D0242277C50021B0B7 /* NeumorphismButtonView.swift */; };
DA5D60D324239E090021B0B7 /* BindingButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5D60D224239E090021B0B7 /* BindingButtonView.swift */; };
DA6FEED324311E5800C1B554 /* NeumorphismSliderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA6FEED224311E5700C1B554 /* NeumorphismSliderView.swift */; };
Expand Down Expand Up @@ -48,7 +48,7 @@
DA1A6B7B240B81A6001EA5EB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DA5AFAD22418367C0098E0BB /* SimpleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleView.swift; sourceTree = "<group>"; };
DA5D60CB2422727E0021B0B7 /* BaseView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseView.swift; sourceTree = "<group>"; };
DA5D60CE2422767E0021B0B7 /* DentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DentView.swift; sourceTree = "<group>"; };
DA5D60CE2422767E0021B0B7 /* Concave.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Concave.swift; sourceTree = "<group>"; };
DA5D60D0242277C50021B0B7 /* NeumorphismButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismButtonView.swift; sourceTree = "<group>"; };
DA5D60D224239E090021B0B7 /* BindingButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BindingButtonView.swift; sourceTree = "<group>"; };
DA6FEED224311E5700C1B554 /* NeumorphismSliderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismSliderView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -112,7 +112,7 @@
isa = PBXGroup;
children = (
DA5AFAD22418367C0098E0BB /* SimpleView.swift */,
DA5D60CE2422767E0021B0B7 /* DentView.swift */,
DA5D60CE2422767E0021B0B7 /* Concave.swift */,
DA5D60D224239E090021B0B7 /* BindingButtonView.swift */,
DA6FEED224311E5700C1B554 /* NeumorphismSliderView.swift */,
DA5D60D0242277C50021B0B7 /* NeumorphismButtonView.swift */,
Expand Down Expand Up @@ -205,7 +205,7 @@
DA1A6B6E240B81A4001EA5EB /* AppDelegate.swift in Sources */,
DA1A6B70240B81A4001EA5EB /* SceneDelegate.swift in Sources */,
DA5D60D324239E090021B0B7 /* BindingButtonView.swift in Sources */,
DA5D60CF2422767E0021B0B7 /* DentView.swift in Sources */,
DA5D60CF2422767E0021B0B7 /* Concave.swift in Sources */,
DA1A6B72240B81A4001EA5EB /* ContentView.swift in Sources */,
DA6FEED324311E5800C1B554 /* NeumorphismSliderView.swift in Sources */,
DA5D60CC2422727E0021B0B7 /* BaseView.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 @@ -36,8 +36,8 @@ struct ContentView: View {
BaseView(name: "SimpleView", desc: "most simple neumorphism")
}.padding()

NavigationLink(destination: DentView()) {
BaseView(name: "DentView", desc: "dent neumorphism")
NavigationLink(destination: Concave()) {
BaseView(name: "Concave", desc: "concave neumorphism")
}.padding()

NavigationLink(destination: NeumorphismButtonView()) {
Expand Down
47 changes: 47 additions & 0 deletions DemoApp/DemoApp/Views/Concave.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import SwiftUI
import NeumorphismUI

struct Concave: View {
@EnvironmentObject var neumorphism: NeumorphismManager

var body: some View {
ZStack {
neumorphism.color.edgesIgnoringSafeArea(.all)
VStack(spacing: 16) {
Rectangle()
.fill(neumorphism.color)
.neumorphismConcave(shapeType: .rectangle, color: nil)
.frame(width: 256, height: 100)

RoundedRectangle(cornerRadius: 16)
.fill(neumorphism.color)
.neumorphismConcave(shapeType: .roundedRectangle(cornerRadius: 16), color: nil)
.frame(width: 256, height: 100)

Circle()
.fill(neumorphism.color)
.neumorphismConcave(shapeType: .circle, color: nil)
.frame(width: 150, height: 150)

Ellipse()
.fill(neumorphism.color)
.neumorphismConcave(shapeType: .ellipse, color: nil)
.frame(width: 300, height: 100)


}
}
}
}

struct DentView_Previews: PreviewProvider {
static let neumorphism = NeumorphismManager(
isDark: false,
lightColor: Color(hex: "C1D2EB"),
darkColor: Color(hex: "2C292C")
)
static var previews: some View {
Concave()
.environmentObject(neumorphism)
}
}
25 changes: 0 additions & 25 deletions DemoApp/DemoApp/Views/DentView.swift

This file was deleted.

4 changes: 4 additions & 0 deletions NeumorphismUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
DA14F56F2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA14F56E2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift */; };
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 */; };
Expand Down Expand Up @@ -61,6 +62,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
DA14F56E2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismConcaveModifier.swift; sourceTree = "<group>"; };
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>"; };
Expand Down Expand Up @@ -110,6 +112,7 @@
isa = PBXGroup;
children = (
OBJ_13 /* NeumorphismShadowModifier.swift */,
DA14F56E2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift */,
);
path = Modifier;
sourceTree = "<group>";
Expand Down Expand Up @@ -293,6 +296,7 @@
buildActionMask = 0;
files = (
DAD95493241B4E0200D3F56E /* NeumorphismLabelButton.swift in Sources */,
DA14F56F2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift in Sources */,
OBJ_30 /* ColorTransformer.swift in Sources */,
OBJ_31 /* ColorExtension.swift in Sources */,
DA98731B240BDE7D0083EB5E /* HighlightableButton.swift in Sources */,
Expand Down
42 changes: 17 additions & 25 deletions Sources/CustomViews/NeumorphismDentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,31 @@ import SwiftUI
public struct NeumorphismDentView: View {
@EnvironmentObject var neumorphism: NeumorphismManager

private var shapeType: ShapeType
private let width: CGFloat?
private let height: CGFloat?
private let color: Color?
private let cornerRadius: CGFloat

public init(
shapeType: ShapeType = .roundedRectangle(cornerRadius: 16),
width: CGFloat? = nil,
height: CGFloat? = nil,
color: Color? = nil,
cornerRadius: CGFloat = 16
) {
self.shapeType = shapeType
self.width = width
self.height = height
self.color = color
self.cornerRadius = cornerRadius
}

public var body: some View {
ZStack{
RoundedRectangle(cornerRadius: self.cornerRadius)
.fill(self.color ?? self.neumorphism.color)
.frame(width: self.width, height: self.height)
.overlay(
RoundedRectangle(cornerRadius: self.cornerRadius)
.stroke(self.color ?? self.neumorphism.color, lineWidth: 4)
.shadow(
color: self.color?.darkerColor() ?? self.neumorphism.color.darkerColor(),
radius: 4,
x: 4,
y: 4)
.clipShape(RoundedRectangle(cornerRadius: self.cornerRadius))
.shadow(
color: self.color?.lighterColor() ?? self.neumorphism.color.lighterColor(),
radius: 2,
x: -2,
y: -2)
.clipShape(RoundedRectangle(cornerRadius: self.cornerRadius))
)
.cornerRadius(self.cornerRadius)
}
Rectangle()
.fill(self.color ?? self.neumorphism.color)
.frame(width: self.width, height: self.height)
.neumorphismConcave(shapeType: shapeType, color: color)
}
}

Expand All @@ -54,8 +39,15 @@ struct NeumorphismTextView_Previews: PreviewProvider {
darkColor: Color(hex: "2C292C")
)
static var previews: some View {
NeumorphismDentView()
.environmentObject(neumorphism)
.previewLayout(.sizeThatFits)
ZStack {
neumorphism.color
NeumorphismDentView()
.frame(width: 100, height: 100)
.environmentObject(neumorphism)
.padding()
}
.frame(width: 100, height: 100)
.padding()
.previewLayout(.sizeThatFits)
}
}
3 changes: 1 addition & 2 deletions Sources/CustomViews/NeumorphismSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public struct NeumorphismSlider: View {
ZStack(alignment: .leading) {
NeumorphismDentView(
width: width,
height: height,
cornerRadius: self.height / 2
height: height
)

ZStack (alignment: .trailing){
Expand Down
9 changes: 9 additions & 0 deletions Sources/Extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ public extension View {
isAnimation: isAnimation
))
}

func neumorphismConcave(
shapeType: ShapeType = .circle,
color: Color? = nil
) -> some View {
self.modifier(NeumorphismConcaveModifier(
shapeType: shapeType, color: color
))
}
}
49 changes: 49 additions & 0 deletions Sources/Modifier/NeumorphismConcaveModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import SwiftUI

@available(iOS 13.0, *)
struct NeumorphismConcaveModifier: ViewModifier {
@EnvironmentObject var neumorphism: NeumorphismManager

private var shapeType: ShapeType
private let color: Color?

init(shapeType: ShapeType = .circle, color: Color? = nil) {
self.shapeType = shapeType
self.color = color
}

func body(content: Content) -> some View {
content
.overlay(
shapeType.anyShape
.stroke(color ?? neumorphism.color, lineWidth: 4)
.shadow(color: color?.darkerColor() ?? neumorphism.color.darkerColor(),
radius: 4, x: 4, y: 4)
.shadow(color: color?.lighterColor() ?? neumorphism.color.lighterColor(),
radius: 4, x: -4, y: -4))
.clipShape(shapeType.anyShape)

}
}

struct NeumorphismConcaveModifier_Previews: PreviewProvider {
static let neumorphism = NeumorphismManager(
lightColor: Color(hex: "C1D2EB"),
darkColor: Color(hex: "2C292C")
)
static var previews: some View {
ZStack {
neumorphism.color
Circle()
.fill(neumorphism.color)
.frame(width: 100, height: 100)
.modifier(NeumorphismConcaveModifier(shapeType: .circle))
.environmentObject(neumorphism)
.padding()
}
.frame(width: 100, height: 100)
.padding()
.previewLayout(.sizeThatFits)

}
}

0 comments on commit f9f409b

Please sign in to comment.