Skip to content

Commit

Permalink
NeumorphismSlider
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed Mar 30, 2020
1 parent 83cb5f3 commit 9587b5f
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DemoApp/DemoApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DA5D60CF2422767E0021B0B7 /* DentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5D60CE2422767E0021B0B7 /* DentView.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 */; };
DA74B267240B820400AD9A06 /* NeumorphismUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA74B266240B820400AD9A06 /* NeumorphismUI.framework */; };
DA74B268240B820400AD9A06 /* NeumorphismUI.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DA74B266240B820400AD9A06 /* NeumorphismUI.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -50,6 +51,7 @@
DA5D60CE2422767E0021B0B7 /* DentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DentView.swift; sourceTree = "<group>"; };
DA5D60D0242277C50021B0B7 /* NeumorphismButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismButtonView.swift; sourceTree = "<group>"; };
DA5D60D224239E090021B0B7 /* BindingButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BindingButtonView.swift; sourceTree = "<group>"; };
DA6FEED224311E5700C1B554 /* NeumorphismSliderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismSliderView.swift; sourceTree = "<group>"; };
DA74B266240B820400AD9A06 /* NeumorphismUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = NeumorphismUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -112,6 +114,7 @@
DA5AFAD22418367C0098E0BB /* SimpleView.swift */,
DA5D60CE2422767E0021B0B7 /* DentView.swift */,
DA5D60D224239E090021B0B7 /* BindingButtonView.swift */,
DA6FEED224311E5700C1B554 /* NeumorphismSliderView.swift */,
DA5D60D0242277C50021B0B7 /* NeumorphismButtonView.swift */,
);
path = Views;
Expand Down Expand Up @@ -204,6 +207,7 @@
DA5D60D324239E090021B0B7 /* BindingButtonView.swift in Sources */,
DA5D60CF2422767E0021B0B7 /* DentView.swift in Sources */,
DA1A6B72240B81A4001EA5EB /* ContentView.swift in Sources */,
DA6FEED324311E5800C1B554 /* NeumorphismSliderView.swift in Sources */,
DA5D60CC2422727E0021B0B7 /* BaseView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
4 changes: 4 additions & 0 deletions DemoApp/DemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ struct ContentView: View {
BaseView(name: "NeumorphismBindingButton", desc: "neumorphism binding button")
}.padding()

NavigationLink(destination: NeumorphismSliderView()) {
BaseView(name: "NeumorphismSlider", desc: "custom slider")
}.padding()

Spacer()
}
}.background(self.neumorphism.color.edgesIgnoringSafeArea(.all))
Expand Down
29 changes: 29 additions & 0 deletions DemoApp/DemoApp/Views/NeumorphismSliderView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import SwiftUI
import NeumorphismUI

struct NeumorphismSliderView: View {
@EnvironmentObject var neumorphism: NeumorphismManager
@State private var value: Double = 0

var body: some View {
ZStack {
neumorphism.color.edgesIgnoringSafeArea(.all)
VStack {
Text("VALUE: \(value)")
.foregroundColor(self.neumorphism.fontColor())

NeumorphismSlider(
tipColor: neumorphism.color.darkerColor().darkerColor(),
barColor: nil,
value: $value
)
}
}
}
}

struct NeumorphismSliderView_Previews: PreviewProvider {
static var previews: some View {
NeumorphismSliderView()
}
}
4 changes: 4 additions & 0 deletions NeumorphismUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 */; };
DA6FEED12430B26C00C1B554 /* NeumorphismSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA6FEED02430B26C00C1B554 /* NeumorphismSlider.swift */; };
DA90AF652412D01900E759A9 /* NeumorphismManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA90AF642412D01900E759A9 /* NeumorphismManagerTests.swift */; };
DA90AF682412D0EE00E759A9 /* NeumorphismManagerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA90AF672412D0EE00E759A9 /* NeumorphismManagerMock.swift */; };
DA954C782411D9740052B8FF /* NeumorphismManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA954C772411D9740052B8FF /* NeumorphismManager.swift */; };
Expand Down Expand Up @@ -64,6 +65,7 @@
DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismButton.swift; sourceTree = "<group>"; };
DA5AFACC2418332A0098E0BB /* ShapeType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapeType.swift; sourceTree = "<group>"; };
DA5AFACE241833430098E0BB /* AnyShape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyShape.swift; sourceTree = "<group>"; };
DA6FEED02430B26C00C1B554 /* NeumorphismSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismSlider.swift; sourceTree = "<group>"; };
DA90AF642412D01900E759A9 /* NeumorphismManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismManagerTests.swift; sourceTree = "<group>"; };
DA90AF672412D0EE00E759A9 /* NeumorphismManagerMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismManagerMock.swift; sourceTree = "<group>"; };
DA954C772411D9740052B8FF /* NeumorphismManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeumorphismManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -137,6 +139,7 @@
DA5AFAC8241691210098E0BB /* NeumorphismButton.swift */,
DAD9549A241BCFBE00D3F56E /* NeumorphismBindingButton.swift */,
DAD95492241B4E0200D3F56E /* NeumorphismLabelButton.swift */,
DA6FEED02430B26C00C1B554 /* NeumorphismSlider.swift */,
);
path = CustomViews;
sourceTree = "<group>";
Expand Down Expand Up @@ -301,6 +304,7 @@
OBJ_32 /* ViewExtension.swift in Sources */,
DAD95499241BBD7500D3F56E /* NeumorphismCircleDentView.swift in Sources */,
OBJ_33 /* NeumorphismShadowModifier.swift in Sources */,
DA6FEED12430B26C00C1B554 /* NeumorphismSlider.swift in Sources */,
DA5AFACF241833430098E0BB /* AnyShape.swift in Sources */,
DAD95495241B740E00D3F56E /* NeumorphismDentView.swift in Sources */,
DAD95497241B855F00D3F56E /* UIColorExtensioin.swift in Sources */,
Expand Down
106 changes: 106 additions & 0 deletions Sources/CustomViews/NeumorphismSlider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import SwiftUI

@available(iOS 13.0, *)
public struct NeumorphismSlider: View {
@EnvironmentObject var neumorphism: NeumorphismManager
@Binding var value: Double
private var tipColor: Color?
private let barColor: Color?
private let height: CGFloat
private let width: CGFloat

public init(
width: CGFloat = 300,
height: CGFloat = 16,
tipColor: Color? = nil,
barColor: Color? = nil,
value: Binding<Double>
) {
self.height = height
self.width = width
self.tipColor = tipColor
self.barColor = barColor
self._value = value

validate()
}

public var body: some View {
ZStack(alignment: .leading) {
NeumorphismDentView(
width: width,
height: height,
cornerRadius: self.height / 2
)

ZStack (alignment: .trailing){
RoundedRectangle(cornerRadius: self.height / 2)
.fill(
LinearGradient(gradient:
Gradient(colors:
[barColor ?? self.neumorphism.color.darkerColor(),
barColor?.darkerColor() ?? self.neumorphism.color.darkerColor().darkerColor()]),
startPoint: .leading,
endPoint: .trailing))
.frame(
width: self.width * CGFloat(value),
height: height * 0.9,
alignment: .leading)
.padding(.leading, 4)
HStack {
ZStack {
Circle()
.fill(self.neumorphism.color)
.frame(width: height * 2, height: height * 2)
.neumorphismShadow()

NeumorphismDentView(
width: height * 0.8,
height: height * 0.8,
color: tipColor ?? self.neumorphism.color.darkerColor(),
cornerRadius: height/2)
}
}
}
}.gesture(
DragGesture(minimumDistance: 0)
.onChanged({ (value) in
self.value = Double(value.location.x / self.width)
self.validate()
})
.onEnded({ (value) in
self.value = Double(value.location.x / self.width)
self.validate()
})
)
}

private func validate() {
if self.value > 1.0 {
DispatchQueue.main.async {
self.value = 1.0
}
}
if self.value < 0 {
DispatchQueue.main.async {
self.value = 0
}
}
}
}

@available(iOS 13.0, *)
public struct NeumorphismSlider_Previews: PreviewProvider {
static let neumorphism = NeumorphismManager(
lightColor: Color(hex: "C1D2EB"),
darkColor: Color(hex: "2C292C")
)
public static var previews: some View {
NeumorphismSlider(
width: 300,
height: 16,
value: .constant(0.4))
.environmentObject(neumorphism)
.previewLayout(.sizeThatFits)
}
}

0 comments on commit 9587b5f

Please sign in to comment.