Skip to content

Commit

Permalink
Added SoftButtonPressedEffect
Browse files Browse the repository at this point in the history
Added three types of pressed effects to softButtonStyle.
Added SoftButtonDemoView in the example project.
  • Loading branch information
costachung committed Aug 10, 2020
1 parent 61afef9 commit 2ab34ef
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 21 deletions.
46 changes: 29 additions & 17 deletions Sources/Neumorphic/SoftButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

import SwiftUI

public enum SoftButtonPressedEffect {
case none
case flat
case hard
}

public struct SoftDynamicButtonStyle<S: Shape> : ButtonStyle {

Expand All @@ -14,34 +19,40 @@ public struct SoftDynamicButtonStyle<S: Shape> : ButtonStyle {
var textColor : Color
var darkShadowColor : Color
var lightShadowColor : Color
var pressedEffect : SoftButtonPressedEffect

public init(_ shape: S, mainColor : Color, textColor : Color, darkShadowColor: Color, lightShadowColor: Color) {
public init(_ shape: S, mainColor : Color, textColor : Color, darkShadowColor: Color, lightShadowColor: Color, pressedEffect : SoftButtonPressedEffect) {
self.shape = shape
self.mainColor = mainColor
self.textColor = textColor
self.darkShadowColor = darkShadowColor
self.lightShadowColor = lightShadowColor
self.pressedEffect = pressedEffect
}

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)
}
)
}

.background(
ZStack{
if pressedEffect == .flat {
shape.stroke(darkShadowColor, lineWidth : configuration.isPressed ? 1 : 0)
.opacity(configuration.isPressed ? 1 : 0)
shape.fill(mainColor)
}
else if pressedEffect == .hard {
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(pressedEffect == .none ? 1 : (configuration.isPressed ? 0 : 1) )
}
)
}

}
Expand Down Expand Up @@ -86,9 +97,10 @@ public struct SoftButtonStyle<S: Shape> : ButtonStyle {

extension Button {

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(SoftDynamicButtonStyle(content, mainColor: mainColor, textColor: textColor, darkShadowColor: darkShadowColor, lightShadowColor: 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(), pressedEffect : SoftButtonPressedEffect = .hard) -> some View {
self.buttonStyle(SoftDynamicButtonStyle(content, mainColor: mainColor, textColor: textColor, darkShadowColor: darkShadowColor, lightShadowColor: lightShadowColor, pressedEffect : pressedEffect))
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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 */; };
AA5ABDD624E0B3EF00622BAF /* SoftButtonDemoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA5ABDD524E0B3EF00622BAF /* SoftButtonDemoView.swift */; };
AAB9DDA82480D0500047A0DC /* Neumorphic in Frameworks */ = {isa = PBXBuildFile; productRef = AAB9DDA72480D0500047A0DC /* Neumorphic */; };
/* End PBXBuildFile section */

Expand All @@ -25,6 +26,7 @@
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>"; };
AA5ABDD524E0B3EF00622BAF /* SoftButtonDemoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoftButtonDemoView.swift; sourceTree = "<group>"; };
AAB9DDA62480D03B0047A0DC /* neumorphic */ = {isa = PBXFileReference; lastKnownFileType = folder; name = neumorphic; path = ..; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -64,6 +66,7 @@
AA1C843B240CDD090082F452 /* AppDelegate.swift */,
AA1C843D240CDD090082F452 /* SceneDelegate.swift */,
AA1C843F240CDD090082F452 /* ContentView.swift */,
AA5ABDD524E0B3EF00622BAF /* SoftButtonDemoView.swift */,
AA1C8441240CDD090082F452 /* Assets.xcassets */,
AA1C8446240CDD090082F452 /* LaunchScreen.storyboard */,
AA1C8449240CDD090082F452 /* Info.plist */,
Expand Down Expand Up @@ -164,6 +167,7 @@
AA1C843C240CDD090082F452 /* AppDelegate.swift in Sources */,
AA1C843E240CDD090082F452 /* SceneDelegate.swift in Sources */,
AA1C8440240CDD090082F452 /* ContentView.swift in Sources */,
AA5ABDD624E0B3EF00622BAF /* SoftButtonDemoView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
11 changes: 7 additions & 4 deletions neumorphic-ios-example/neumorphic-ios-example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ struct ContentView: View {

Rectangle().fill(mainColor).frame(width: 90, height: 90)
.softOuterShadow()

}

//You can simply create soft button with softButtonStyle method.
Button(action: {}) {
Text("Soft Button").fontWeight(.bold).frame(width: 300, height:20)
}
.softButtonStyle(RoundedRectangle(cornerRadius: cornerRadius))
Text("Soft Button").fontWeight(.bold)
}.softButtonStyle(RoundedRectangle(cornerRadius: cornerRadius))

HStack(spacing: 30) {
HStack(spacing: 20) {
//Circle Button
Button(action: {}) {
Image(systemName: "heart.fill")
}.softButtonStyle(Circle())


//Ellipse Button
Button(action: {}) {
Text("Thanks").fontWeight(.bold).frame(width: 150, height: 20)
Expand Down Expand Up @@ -100,9 +101,11 @@ struct ContentView_Previews: PreviewProvider {

ContentView()
.environment(\.colorScheme, .dark)

}
}
}




112 changes: 112 additions & 0 deletions neumorphic-ios-example/neumorphic-ios-example/SoftButtonDemoView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// SoftButtonDemoView.swift
// neumorphic-ios-example
//
// Created by Costa Chung on 10/8/2020.
// Copyright © 2020 Costa Chung. All rights reserved.
//

import SwiftUI
import Neumorphic

struct SoftButtonDemoView: View {

@Environment(\.colorScheme) var colorScheme: ColorScheme

var body: some View {
Neumorphic.shared.colorScheme = colorScheme
return NavigationView {
ZStack {
Neumorphic.shared.mainColor().edgesIgnoringSafeArea(.all)

ScrollView {
Spacer(minLength: 15)

HStack {
Button(action: {}) {
Text("Capsule").fontWeight(.bold)
}.softButtonStyle(Capsule())
.padding(10)

Button(action: {}) {
Text("RoundedRectangle").fontWeight(.bold)
}.softButtonStyle(RoundedRectangle(cornerRadius: 20))
.padding(10)
}

HStack {
Button(action: {}) {
Image(systemName: "heart.fill")
}.softButtonStyle(Circle())
.padding(10)

Button(action: {}) {
Text("Ellipse").fontWeight(.bold).frame(width: 150, height: 20)
}.softButtonStyle(Ellipse())
.padding(10)

Button(action: {}) {
Image(systemName: "heart.fill")
}.softButtonStyle(Circle(), mainColor: Color.red, textColor: Color.white, darkShadowColor: Color(rgb: 0x993333, alpha: 1), lightShadowColor:Color("redButtonLightShadow"))
.padding(10)
}

Spacer(minLength: 95)

Text("Pressed Effect").font(.headline).foregroundColor(Neumorphic.shared.secondaryColor())

HStack {
Spacer()
Button(action: {}) {
Text(".none").fontWeight(.bold)
}.softButtonStyle(Capsule(), pressedEffect: .none)
Spacer()
Button(action: {}) {
Text(".flat").fontWeight(.bold)
}.softButtonStyle(Capsule(), pressedEffect: .flat)
Spacer()
Button(action: {}) {
Text(".hard").fontWeight(.bold)
}.softButtonStyle(Capsule(), pressedEffect: .hard)
Spacer()
}

Spacer(minLength: 95)

Text("ContextMenu").font(.headline).foregroundColor(Neumorphic.shared.secondaryColor())

Button(action: {}) {
Text("Button").fontWeight(.bold)
}
.softButtonStyle(Capsule(), pressedEffect: .none)
.contentShape(Capsule())
.contextMenu(
ContextMenu(menuItems: {
Text("Menu Item 1")
Text("Menu Item 2")
}
))
.background(
Capsule().fill(Neumorphic.shared.mainColor()).softOuterShadow()
)




}.navigationBarTitle("Soft Button Demo")
}
}
}
}

struct SoftButtonDemoView_Previews: PreviewProvider {
static var previews: some View {
Group {
SoftButtonDemoView()
.environment(\.colorScheme, .light)

SoftButtonDemoView()
.environment(\.colorScheme, .dark)
}
}
}

0 comments on commit 2ab34ef

Please sign in to comment.