Skip to content

Commit

Permalink
New Feature: Added new background enum to change style of background (#…
Browse files Browse the repository at this point in the history
…56)

* New Feature: Added new background enum to change style of background of sheet. Added few extra examples
  • Loading branch information
styrken authored Aug 16, 2020
1 parent b608c39 commit db2fe55
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 32 deletions.
4 changes: 4 additions & 0 deletions Example/PartialSheetExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
1F177AA623E1F0E0006F59D0 /* PartialSheetViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F177AA323E1F0E0006F59D0 /* PartialSheetViewModifier.swift */; };
1F177AA723E1F0E0006F59D0 /* DragState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F177AA423E1F0E0006F59D0 /* DragState.swift */; };
1F177AA823E1F0E0006F59D0 /* View+PartialSheetModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F177AA523E1F0E0006F59D0 /* View+PartialSheetModifier.swift */; };
3BF874DF24E6F4DE004F4550 /* BlurredSheetExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -45,6 +46,7 @@
1F177AA323E1F0E0006F59D0 /* PartialSheetViewModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PartialSheetViewModifier.swift; sourceTree = "<group>"; };
1F177AA423E1F0E0006F59D0 /* DragState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DragState.swift; sourceTree = "<group>"; };
1F177AA523E1F0E0006F59D0 /* View+PartialSheetModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+PartialSheetModifier.swift"; sourceTree = "<group>"; };
3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurredSheetExample.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -65,6 +67,7 @@
01A013952458E4C000D0F5DD /* TextfieldExample.swift */,
01A013982458E4D800D0F5DD /* ListExample.swift */,
0174F416245962B80053C454 /* PushNavigationExample.swift */,
3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */,
);
path = Examples;
sourceTree = "<group>";
Expand Down Expand Up @@ -197,6 +200,7 @@
01CCB63C244AEE3900F7F67F /* View+IfDeviceType.swift in Sources */,
01294541244ED5E5006190B0 /* PartialSheetStyle.swift in Sources */,
0174F417245962B80053C454 /* PushNavigationExample.swift in Sources */,
3BF874DF24E6F4DE004F4550 /* BlurredSheetExample.swift in Sources */,
01A013962458E4C000D0F5DD /* TextfieldExample.swift in Sources */,
01A013942458E4A900D0F5DD /* NormalExample.swift in Sources */,
1F177A6E23E1ECC3006F59D0 /* AppDelegate.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions Example/PartialSheetExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ struct ContentView: View {
label: {Text("Push Navigation Example")

})
NavigationLink(
destination: BlurredExample(),
label: {Text("Blurred Example")

})
}
Spacer()
Spacer()
Expand Down
77 changes: 77 additions & 0 deletions Example/PartialSheetExample/Examples/BlurredSheetExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// BlurredSheetExample.swift
// PartialSheetExample
//
// Created by Rasmus Styrk on 14/08/2020.
// Copyright © 2020 Swift. All rights reserved.
//

import SwiftUI

struct BlurredExample: View {

// Adding a new sheet manager and style locally - *this is not recommended (it's just for the example sake)*
// - recommended way is adding one global sheet as per the documentation.
let sheetManager: PartialSheetManager = PartialSheetManager()
let sheetStyle = PartialSheetStyle(background: .blur(.systemMaterialDark),
handlerBarColor: Color(UIColor.systemGray2),
enableCover: true,
coverColor: Color.black.opacity(0.4),
blurEffectStyle: nil,
cornerRadius: 10
)

var body: some View {
VStack {
Spacer()
Button(action: {
self.sheetManager.showPartialSheet({
print("BlurredExample sheet dismissed")
}) {
BlurredSheetView()
}
}, label: {
Text("Display the BlurredExample Sheet")
})
.padding()
Spacer()
HStack {
Rectangle().foregroundColor(Color.blue).frame(width: 75, height: 75)
Rectangle().foregroundColor(Color.green).frame(width: 75, height: 75)
Rectangle().foregroundColor(Color.red).frame(width: 75, height: 75)
Rectangle().foregroundColor(Color.yellow).frame(width: 75, height: 75)
}
}
.navigationBarTitle("BlurredExample Example")
.navigationViewStyle(StackNavigationViewStyle())
.addPartialSheet(style: self.sheetStyle)
.environmentObject(self.sheetManager)
}
}

struct BlurredExample_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
BlurredSheetView()
}
.addPartialSheet()
.navigationViewStyle(StackNavigationViewStyle())
.environmentObject(PartialSheetManager())
}
}

struct BlurredSheetView: View {
@State private var selectedStrength = 0

var body: some View {
VStack {
Group {
Text("Settings Panel").font(.headline).foregroundColor(Color.white)
Text("The background of the sheet is blurred").font(.subheadline).foregroundColor(Color.white)
}
.padding()
.frame(height: 50)
}
}
}

41 changes: 11 additions & 30 deletions Sources/PartialSheet/PartialSheetStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@

import SwiftUI


public struct PartialSheetStyle {

/// The color of the background
var backgroundColor: Color
/// Background enum
public enum PartialSheetBackground {
case solid(Color)
case blur(UIBlurEffect.Style)
}

/// The background of the sheet
var background: PartialSheetBackground

/// The color of the Handlander Bar and the X button on ipad and mac
var handlerBarColor: Color
Expand All @@ -27,46 +34,20 @@ public struct PartialSheetStyle {

/// The corner radius of Sheet
var cornerRadius: CGFloat

/**
The **Style** for the PartialSheet
- parameter backgroundColor: The background color of the partial sheet
- parameter handlerBarColor: The color of the handler bar to close the partial sheet
- parameter enableCover: True if you want a cover enabled between the sheet and the presenter view.
- parameter coverColor: The color of the cover, use the .opacity modifier if you want a transparent effect
- parameter blurEffectStyle: If you want a blur effect on the cover, set the effect style, otherwise put it to nil.
Use `PartialSheetStyle.defaultStyle` if you want a quicker init for the style with default values.
*/
public init(
backgroundColor: Color,
handlerBarColor: Color,
enableCover: Bool,
coverColor: Color,
blurEffectStyle: UIBlurEffect.Style?,
cornerRadius: CGFloat
) {
self.backgroundColor = backgroundColor
self.handlerBarColor = handlerBarColor
self.enableCover = enableCover
self.coverColor = coverColor
self.blurEffectStyle = blurEffectStyle
self.cornerRadius = cornerRadius
}
}

extension PartialSheetStyle {

/** A default Style for the PartialSheet with system colors.
- backgroundColor: Color(UIColor.tertiarySystemBackground)
- background: .solid(Color(UIColor.tertiarySystemBackground))
- handlerBarColor: Color(UIColor.systemGray2)
- enableCover: true
- coverColor: Color.black.opacity(0.4)
- blurEffectStyle: nil
*/
public static func defaultStyle() -> PartialSheetStyle {
return PartialSheetStyle(backgroundColor: Color(UIColor.tertiarySystemBackground),
return PartialSheetStyle(background: .solid(Color(UIColor.tertiarySystemBackground)),
handlerBarColor: Color(UIColor.systemGray2),
enableCover: true,
coverColor: Color.black.opacity(0.4),
Expand Down
14 changes: 12 additions & 2 deletions Sources/PartialSheet/PartialSheetViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ struct PartialSheet: ViewModifier {

/// The Gesture State for the drag gesture
@GestureState private var dragState = DragState.inactive

/// Background of sheet
private var background: AnyView {
switch self.style.background {
case .solid(let color):
return AnyView(color)
case .blur(let effect):
return AnyView(BlurEffectView(style: effect).background(Color.clear))
}
}

// MARK: - Content Builders

Expand Down Expand Up @@ -153,7 +163,7 @@ extension PartialSheet {
}
self.manager.content
Spacer()
}
}.background(self.background)
}

//MARK: - iPhone Sheet Builder
Expand Down Expand Up @@ -214,7 +224,7 @@ extension PartialSheet {
self.sheetContentRect = prefData.first?.bounds ?? .zero
})
.frame(width: UIScreen.main.bounds.width)
.background(style.backgroundColor)
.background(self.background)
.cornerRadius(style.cornerRadius)
.shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.13), radius: 10.0)
.offset(y: self.sheetPosition)
Expand Down

0 comments on commit db2fe55

Please sign in to comment.