diff --git a/DemoApp/DemoApp.xcodeproj/project.pbxproj b/DemoApp/DemoApp.xcodeproj/project.pbxproj index dd4a42b..969c3b5 100644 --- a/DemoApp/DemoApp.xcodeproj/project.pbxproj +++ b/DemoApp/DemoApp.xcodeproj/project.pbxproj @@ -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 */; }; @@ -48,7 +48,7 @@ DA1A6B7B240B81A6001EA5EB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; DA5AFAD22418367C0098E0BB /* SimpleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleView.swift; sourceTree = ""; }; DA5D60CB2422727E0021B0B7 /* BaseView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseView.swift; sourceTree = ""; }; - DA5D60CE2422767E0021B0B7 /* DentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DentView.swift; sourceTree = ""; }; + DA5D60CE2422767E0021B0B7 /* Concave.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Concave.swift; sourceTree = ""; }; DA5D60D0242277C50021B0B7 /* NeumorphismButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismButtonView.swift; sourceTree = ""; }; DA5D60D224239E090021B0B7 /* BindingButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BindingButtonView.swift; sourceTree = ""; }; DA6FEED224311E5700C1B554 /* NeumorphismSliderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismSliderView.swift; sourceTree = ""; }; @@ -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 */, @@ -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 */, diff --git a/DemoApp/DemoApp/ContentView.swift b/DemoApp/DemoApp/ContentView.swift index c3c32d4..f78d573 100644 --- a/DemoApp/DemoApp/ContentView.swift +++ b/DemoApp/DemoApp/ContentView.swift @@ -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()) { diff --git a/DemoApp/DemoApp/Views/Concave.swift b/DemoApp/DemoApp/Views/Concave.swift new file mode 100644 index 0000000..69f47d5 --- /dev/null +++ b/DemoApp/DemoApp/Views/Concave.swift @@ -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) + } +} diff --git a/DemoApp/DemoApp/Views/DentView.swift b/DemoApp/DemoApp/Views/DentView.swift deleted file mode 100644 index 3ddb761..0000000 --- a/DemoApp/DemoApp/Views/DentView.swift +++ /dev/null @@ -1,25 +0,0 @@ -import SwiftUI -import NeumorphismUI - -struct DentView: View { - @EnvironmentObject var neumorphism: NeumorphismManager - - var body: some View { - ZStack { - neumorphism.color.edgesIgnoringSafeArea(.all) - NeumorphismDentView(width: 300, height: 300, cornerRadius: 16) - } - } -} - -struct DentView_Previews: PreviewProvider { - static let neumorphism = NeumorphismManager( - isDark: false, - lightColor: Color(hex: "C1D2EB"), - darkColor: Color(hex: "2C292C") - ) - static var previews: some View { - DentView() - .environmentObject(neumorphism) - } -} diff --git a/NeumorphismUI.xcodeproj/project.pbxproj b/NeumorphismUI.xcodeproj/project.pbxproj index eb4c172..41b5df9 100644 --- a/NeumorphismUI.xcodeproj/project.pbxproj +++ b/NeumorphismUI.xcodeproj/project.pbxproj @@ -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 */; }; @@ -61,6 +62,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + DA14F56E2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismConcaveModifier.swift; sourceTree = ""; }; DA1A6B5F240B76D9001EA5EB /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = ""; }; DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismButton.swift; sourceTree = ""; }; DA5AFACC2418332A0098E0BB /* ShapeType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapeType.swift; sourceTree = ""; }; @@ -110,6 +112,7 @@ isa = PBXGroup; children = ( OBJ_13 /* NeumorphismShadowModifier.swift */, + DA14F56E2458DEFF008BCD97 /* NeumorphismConcaveModifier.swift */, ); path = Modifier; sourceTree = ""; @@ -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 */, diff --git a/Sources/CustomViews/NeumorphismDentView.swift b/Sources/CustomViews/NeumorphismDentView.swift index b1f1b07..49565fd 100644 --- a/Sources/CustomViews/NeumorphismDentView.swift +++ b/Sources/CustomViews/NeumorphismDentView.swift @@ -4,17 +4,20 @@ 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 @@ -22,28 +25,10 @@ public struct NeumorphismDentView: View { } 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) } } @@ -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) } } diff --git a/Sources/CustomViews/NeumorphismSlider.swift b/Sources/CustomViews/NeumorphismSlider.swift index cd9f9ac..1185dcd 100644 --- a/Sources/CustomViews/NeumorphismSlider.swift +++ b/Sources/CustomViews/NeumorphismSlider.swift @@ -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){ diff --git a/Sources/Extensions/ViewExtension.swift b/Sources/Extensions/ViewExtension.swift index 8cd539c..83a6508 100644 --- a/Sources/Extensions/ViewExtension.swift +++ b/Sources/Extensions/ViewExtension.swift @@ -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 + )) + } } diff --git a/Sources/Modifier/NeumorphismConcaveModifier.swift b/Sources/Modifier/NeumorphismConcaveModifier.swift new file mode 100644 index 0000000..6e85eb6 --- /dev/null +++ b/Sources/Modifier/NeumorphismConcaveModifier.swift @@ -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) + + } +}