Skip to content

Commit

Permalink
NeumorphismBindingButton
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed Mar 13, 2020
1 parent e8262af commit a6d3335
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DemoApp/DemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NeumorphismUI

struct ContentView: View {
@EnvironmentObject var neumorphism: NeumorphismManager
@State private var isSelected: Bool = false
@State private var isManager: Bool = false
var body: some View {
GeometryReader { geometry in
Expand All @@ -20,7 +21,7 @@ struct ContentView: View {
.fill(self.neumorphism.color.darkerColor())
.frame(width: 365, height: 1)

NeumorphismButton(shapeType: .circle, normalImage: Image(systemName: "book"), selectedImage: Image(systemName: "book.fill"), width: 48, height: 48, imageWidth: 24, imageHeight: 24, shadowRadius: 4) {
NeumorphismBindingButton(isSelected: self.$isSelected, shapeType: .circle, normalImage: Image(systemName: "book"), selectedImage: Image(systemName: "book.fill"), width: 48, height: 48, imageWidth: 24, imageHeight: 24, shadowRadius: 4) {

}
.padding()
Expand Down
4 changes: 4 additions & 0 deletions NeumorphismUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
DAD95495241B740E00D3F56E /* NeumorphismDentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD95494241B740E00D3F56E /* NeumorphismDentView.swift */; };
DAD95497241B855F00D3F56E /* UIColorExtensioin.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD95496241B855F00D3F56E /* UIColorExtensioin.swift */; };
DAD95499241BBD7500D3F56E /* NeumorphismCircleDentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD95498241BBD7500D3F56E /* NeumorphismCircleDentView.swift */; };
DAD9549B241BCFBE00D3F56E /* NeumorphismBindingButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD9549A241BCFBE00D3F56E /* NeumorphismBindingButton.swift */; };
OBJ_30 /* ColorTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* ColorTransformer.swift */; };
OBJ_31 /* ColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* ColorExtension.swift */; };
OBJ_32 /* ViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* ViewExtension.swift */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
DAD95494241B740E00D3F56E /* NeumorphismDentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismDentView.swift; sourceTree = "<group>"; };
DAD95496241B855F00D3F56E /* UIColorExtensioin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorExtensioin.swift; sourceTree = "<group>"; };
DAD95498241BBD7500D3F56E /* NeumorphismCircleDentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismCircleDentView.swift; sourceTree = "<group>"; };
DAD9549A241BCFBE00D3F56E /* NeumorphismBindingButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismBindingButton.swift; sourceTree = "<group>"; };
"NeumorphismUI::NeumorphismUI::Product" /* NeumorphismUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = NeumorphismUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
"NeumorphismUI::NeumorphismUITests::Product" /* NeumorphismUITests.xctest */ = {isa = PBXFileReference; lastKnownFileType = file; path = NeumorphismUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_11 /* ColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -133,6 +135,7 @@
DAD95494241B740E00D3F56E /* NeumorphismDentView.swift */,
DAD95498241BBD7500D3F56E /* NeumorphismCircleDentView.swift */,
DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */,
DAD9549A241BCFBE00D3F56E /* NeumorphismBindingButton.swift */,
DAD95492241B4E0200D3F56E /* NeumorphismLabelButton.swift */,
);
path = CustomViews;
Expand Down Expand Up @@ -293,6 +296,7 @@
DA90AF682412D0EE00E759A9 /* NeumorphismManagerMock.swift in Sources */,
DA954C782411D9740052B8FF /* NeumorphismManager.swift in Sources */,
DA5AFAC9241691210098E0BB /* NeumorphismButton.swift in Sources */,
DAD9549B241BCFBE00D3F56E /* NeumorphismBindingButton.swift in Sources */,
DA5AFACD2418332A0098E0BB /* ShapeType.swift in Sources */,
OBJ_32 /* ViewExtension.swift in Sources */,
DAD95499241BBD7500D3F56E /* NeumorphismCircleDentView.swift in Sources */,
Expand Down
78 changes: 78 additions & 0 deletions Sources/CustomViews/NeumorphismBindingButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import SwiftUI

@available(iOS 13.0.0, *)
public struct NeumorphismBindingButton: View {
@EnvironmentObject var neumorphism: NeumorphismManager
@Binding var isSelected: Bool

private var shapeType: ShapeType
private var normalImage: Image
private var selectedImage: Image
private var width: CGFloat
private var height: CGFloat
private var imageWidth: CGFloat
private var imageHeight: CGFloat
private var shadowRadius: CGFloat
private var handler: (() -> Void)?

public init
(
isSelected: Binding<Bool>,
shapeType: ShapeType = .circle,
normalImage: Image = Image(systemName: "heart"),
selectedImage: Image = Image(systemName: "heart.fill"),
width: CGFloat = 100,
height: CGFloat = 100,
imageWidth: CGFloat = 60,
imageHeight: CGFloat = 60,
shadowRadius: CGFloat = 8,
handler: (() -> Void)? = nil
) {
self._isSelected = isSelected
self.shapeType = shapeType
self.normalImage = normalImage
self.selectedImage = selectedImage
self.width = width
self.height = height
self.imageWidth = imageWidth
self.imageHeight = imageHeight
self.shadowRadius = shadowRadius
self.handler = handler
}

public var body: some View {
HighlightableButton(action: {
self.isSelected.toggle()
self.handler?()
}) { isHeighlight in
(self.isSelected ? self.selectedImage : self.normalImage)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width:self.imageWidth, height: self.imageWidth)
.foregroundColor(self.neumorphism.fontColor())
.background(
Rectangle()
.clipShape(getAnyShape(type: self.shapeType))
.foregroundColor(self.neumorphism.color)
.frame(width: self.width, height: self.height)
.modifier(self.isSelected ? NeumorphismShadowModifier(radius: self.shadowRadius, isAnimation: self.isSelected ) : NeumorphismShadowModifier(radius: self.shadowRadius, isAnimation: isHeighlight))
)
.padding()
.animation(Animation.spring(response: 0.3, dampingFraction: 0.7, blendDuration: 1))
}
}
}

@available(iOS 13.0.0, *)
struct NeumorphismBindingButton_Previews: PreviewProvider {
static let neumorphism = NeumorphismManager(
lightColor: Color(hex: "C1D2EB"),
darkColor: Color(hex: "2C292C")
)

static var previews: some View {
NeumorphismBindingButton(isSelected: .constant(true))
.environmentObject(neumorphism)
.previewLayout(.sizeThatFits)
}
}

0 comments on commit a6d3335

Please sign in to comment.