Skip to content

Commit

Permalink
Fix the incorrect size of button issue. Update SoftButtonStyle to sup…
Browse files Browse the repository at this point in the history
…port dynamic size of button.
  • Loading branch information
costachung committed Jun 1, 2020
1 parent fc38cf5 commit 76705e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
48 changes: 43 additions & 5 deletions Sources/Neumorphic/SoftButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@

import SwiftUI


public struct SoftDynamicButtonStyle<S: Shape> : 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<S: Shape> : ButtonStyle {

var shape: S
Expand Down Expand Up @@ -45,12 +86,9 @@ public struct SoftButtonStyle<S: Shape> : ButtonStyle {

extension Button {

// public func softButtonDefaultStyle<S : Shape>(_ 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<S : Shape>(_ 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))
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -25,15 +25,15 @@
AA1C8444240CDD090082F452 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
AA1C8447240CDD090082F452 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
AA1C8449240CDD090082F452 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AAB2D0EA24288DED0057BC64 /* neumorphic */ = {isa = PBXFileReference; lastKnownFileType = folder; name = neumorphic; path = ..; sourceTree = "<group>"; };
AAB9DDA62480D03B0047A0DC /* neumorphic */ = {isa = PBXFileReference; lastKnownFileType = folder; name = neumorphic; path = ..; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
AA1C8435240CDD090082F452 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
AA7066332417839100D7F3E3 /* Neumorphic in Frameworks */,
AAB9DDA82480D0500047A0DC /* Neumorphic in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -43,7 +43,7 @@
AA1C842F240CDD090082F452 = {
isa = PBXGroup;
children = (
AAB2D0EA24288DED0057BC64 /* neumorphic */,
AAB9DDA62480D03B0047A0DC /* neumorphic */,
AA1C843A240CDD090082F452 /* neumorphic-ios-example */,
AA1C8439240CDD090082F452 /* Products */,
AA1C844F240CDFC20082F452 /* Frameworks */,
Expand Down Expand Up @@ -104,7 +104,7 @@
);
name = "neumorphic-ios-example";
packageProductDependencies = (
AA7066322417839100D7F3E3 /* Neumorphic */,
AAB9DDA72480D0500047A0DC /* Neumorphic */,
);
productName = "neumorphic-ios-example";
productReference = AA1C8438240CDD090082F452 /* neumorphic-ios-example.app */;
Expand Down Expand Up @@ -361,7 +361,7 @@
/* End XCConfigurationList section */

/* Begin XCSwiftPackageProductDependency section */
AA7066322417839100D7F3E3 /* Neumorphic */ = {
AAB9DDA72480D0500047A0DC /* Neumorphic */ = {
isa = XCSwiftPackageProductDependency;
productName = Neumorphic;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down

0 comments on commit 76705e2

Please sign in to comment.