From 76705e201bb8ed768d6052d78ca90f0582ff08c5 Mon Sep 17 00:00:00 2001 From: costachung Date: Mon, 1 Jun 2020 16:05:33 +0800 Subject: [PATCH] Fix the incorrect size of button issue. Update SoftButtonStyle to support dynamic size of button. --- Sources/Neumorphic/SoftButtonStyle.swift | 48 +++++++++++++++++-- .../project.pbxproj | 12 ++--- .../neumorphic-ios-example/ContentView.swift | 9 ++-- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/Sources/Neumorphic/SoftButtonStyle.swift b/Sources/Neumorphic/SoftButtonStyle.swift index 7bffc0c..b7a8fc9 100644 --- a/Sources/Neumorphic/SoftButtonStyle.swift +++ b/Sources/Neumorphic/SoftButtonStyle.swift @@ -6,6 +6,47 @@ import SwiftUI + +public struct SoftDynamicButtonStyle : ButtonStyle { + + var shape: S + var mainColor : Color + var textColor : Color + var darkShadowColor : Color + var lightShadowColor : Color + + public init(_ shape: S, mainColor : Color, textColor : Color, darkShadowColor: Color, lightShadowColor: Color) { + self.shape = shape + self.mainColor = mainColor + self.textColor = textColor + self.darkShadowColor = darkShadowColor + self.lightShadowColor = lightShadowColor + } + + public func makeBody(configuration: Self.Configuration) -> some View { + ZStack { + configuration.label + .foregroundColor(textColor) + .padding() + .scaleEffect(configuration.isPressed ? 0.97 : 1) + .background( + ZStack { + shape.fill(mainColor) + .softInnerShadow(shape, darkShadow: darkShadowColor, lightShadow: lightShadowColor, spread: 0.15, radius: 3) + .opacity(configuration.isPressed ? 1 : 0) + + shape.fill(mainColor) + .softOuterShadow(darkShadow: darkShadowColor, lightShadow: lightShadowColor, offset: 6, radius: 3) + .opacity(configuration.isPressed ? 0 : 1) + } + ) + } + + } + +} + +@available(*, deprecated, message: "Use SoftDynamicButtonStyle instead") public struct SoftButtonStyle : ButtonStyle { var shape: S @@ -45,12 +86,9 @@ public struct SoftButtonStyle : ButtonStyle { extension Button { -// public func softButtonDefaultStyle(_ content: S) -> some View { -// self.buttonStyle(SoftButtonStyle(content, mainColor: Neumorphic.shared.mainColor(), textColor: Neumorphic.shared.secondaryColor(), darkShadowColor: Neumorphic.shared.darkShadowColor(), lightShadowColor: Neumorphic.shared.lightShadowColor())) -// } - public func softButtonStyle(_ content: S, mainColor : Color = Neumorphic.shared.mainColor(), textColor : Color = Neumorphic.shared.secondaryColor(), darkShadowColor: Color = Neumorphic.shared.darkShadowColor(), lightShadowColor: Color = Neumorphic.shared.lightShadowColor()) -> some View { - self.buttonStyle(SoftButtonStyle(content, mainColor: mainColor, textColor: textColor, darkShadowColor: darkShadowColor, lightShadowColor: lightShadowColor)) + self.buttonStyle(SoftDynamicButtonStyle(content, mainColor: mainColor, textColor: textColor, darkShadowColor: darkShadowColor, lightShadowColor: lightShadowColor)) } + } diff --git a/neumorphic-ios-example/neumorphic-ios-example.xcodeproj/project.pbxproj b/neumorphic-ios-example/neumorphic-ios-example.xcodeproj/project.pbxproj index c848824..2974734 100644 --- a/neumorphic-ios-example/neumorphic-ios-example.xcodeproj/project.pbxproj +++ b/neumorphic-ios-example/neumorphic-ios-example.xcodeproj/project.pbxproj @@ -13,7 +13,7 @@ AA1C8442240CDD090082F452 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AA1C8441240CDD090082F452 /* Assets.xcassets */; }; AA1C8445240CDD090082F452 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AA1C8444240CDD090082F452 /* Preview Assets.xcassets */; }; AA1C8448240CDD090082F452 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA1C8446240CDD090082F452 /* LaunchScreen.storyboard */; }; - AA7066332417839100D7F3E3 /* Neumorphic in Frameworks */ = {isa = PBXBuildFile; productRef = AA7066322417839100D7F3E3 /* Neumorphic */; }; + AAB9DDA82480D0500047A0DC /* Neumorphic in Frameworks */ = {isa = PBXBuildFile; productRef = AAB9DDA72480D0500047A0DC /* Neumorphic */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -25,7 +25,7 @@ AA1C8444240CDD090082F452 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; AA1C8447240CDD090082F452 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; AA1C8449240CDD090082F452 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - AAB2D0EA24288DED0057BC64 /* neumorphic */ = {isa = PBXFileReference; lastKnownFileType = folder; name = neumorphic; path = ..; sourceTree = ""; }; + AAB9DDA62480D03B0047A0DC /* neumorphic */ = {isa = PBXFileReference; lastKnownFileType = folder; name = neumorphic; path = ..; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -33,7 +33,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - AA7066332417839100D7F3E3 /* Neumorphic in Frameworks */, + AAB9DDA82480D0500047A0DC /* Neumorphic in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -43,7 +43,7 @@ AA1C842F240CDD090082F452 = { isa = PBXGroup; children = ( - AAB2D0EA24288DED0057BC64 /* neumorphic */, + AAB9DDA62480D03B0047A0DC /* neumorphic */, AA1C843A240CDD090082F452 /* neumorphic-ios-example */, AA1C8439240CDD090082F452 /* Products */, AA1C844F240CDFC20082F452 /* Frameworks */, @@ -104,7 +104,7 @@ ); name = "neumorphic-ios-example"; packageProductDependencies = ( - AA7066322417839100D7F3E3 /* Neumorphic */, + AAB9DDA72480D0500047A0DC /* Neumorphic */, ); productName = "neumorphic-ios-example"; productReference = AA1C8438240CDD090082F452 /* neumorphic-ios-example.app */; @@ -361,7 +361,7 @@ /* End XCConfigurationList section */ /* Begin XCSwiftPackageProductDependency section */ - AA7066322417839100D7F3E3 /* Neumorphic */ = { + AAB9DDA72480D0500047A0DC /* Neumorphic */ = { isa = XCSwiftPackageProductDependency; productName = Neumorphic; }; diff --git a/neumorphic-ios-example/neumorphic-ios-example/ContentView.swift b/neumorphic-ios-example/neumorphic-ios-example/ContentView.swift index 4d6a6c9..6f28f13 100644 --- a/neumorphic-ios-example/neumorphic-ios-example/ContentView.swift +++ b/neumorphic-ios-example/neumorphic-ios-example/ContentView.swift @@ -62,28 +62,25 @@ struct ContentView: View { //You can simply create soft button with softButtonStyle method. Button(action: {}) { - Text("Soft Button").fontWeight(.bold) + Text("Soft Button").fontWeight(.bold).frame(width: 300, height:20) } .softButtonStyle(RoundedRectangle(cornerRadius: cornerRadius)) - .frame(width: 340, height: 50) HStack(spacing: 30) { //Circle Button Button(action: {}) { Image(systemName: "heart.fill") }.softButtonStyle(Circle()) - .frame(width: 50, height: 50) //Ellipse Button Button(action: {}) { - Text("Thanks").fontWeight(.bold) + Text("Thanks").fontWeight(.bold).frame(width: 150, height: 20) }.softButtonStyle(Ellipse()) - .frame(width: 150, height: 50) + //Circle Button Button(action: {}) { Image(systemName: "heart.fill") }.softButtonStyle(Circle(), mainColor: Color.red, textColor: Color.white, darkShadowColor: Color(rgb: 0x993333, alpha: 1), lightShadowColor:Color("redButtonLightShadow")) - .frame(width: 50, height: 50) }