diff --git a/BottomSheetDemo.xcodeproj/project.pbxproj b/BottomSheetDemo.xcodeproj/project.pbxproj index 3af0a93..704c763 100644 --- a/BottomSheetDemo.xcodeproj/project.pbxproj +++ b/BottomSheetDemo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 6845831F2991403500999C84 /* CustomIntensityVisualEffectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6845831E2991403500999C84 /* CustomIntensityVisualEffectView.swift */; }; 7D05F3052741359800EBDBB1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D05F3042741359800EBDBB1 /* AppDelegate.swift */; }; 7D05F30B2741359C00EBDBB1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7D05F30A2741359C00EBDBB1 /* Assets.xcassets */; }; 7D05F3112741359C00EBDBB1 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7D05F30F2741359C00EBDBB1 /* LaunchScreen.storyboard */; }; @@ -82,6 +83,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 6845831E2991403500999C84 /* CustomIntensityVisualEffectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomIntensityVisualEffectView.swift; sourceTree = ""; }; 7D05F3012741359800EBDBB1 /* BottomSheetDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BottomSheetDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7D05F3042741359800EBDBB1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 7D05F30A2741359C00EBDBB1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -339,6 +341,7 @@ isa = PBXGroup; children = ( 7DD5184E28AA2F46003F3D2A /* UIViewController+Convenience.swift */, + 6845831E2991403500999C84 /* CustomIntensityVisualEffectView.swift */, ); path = Extensions; sourceTree = ""; @@ -534,6 +537,7 @@ 7DA6E0DF274F918E009F5C37 /* BottomSheetPresentationController.swift in Sources */, 7DA6E0E7274F919A009F5C37 /* UIScrollView+MulticastDelegate.swift in Sources */, 7DD5185328AA369E003F3D2A /* BottomSheetConfiguration.swift in Sources */, + 6845831F2991403500999C84 /* CustomIntensityVisualEffectView.swift in Sources */, 7DA6E0E8274F919A009F5C37 /* UINavigationController+MulticastDelegate.swift in Sources */, 7DA6E0E6274F9196009F5C37 /* CGSize+Helpers.swift in Sources */, 7DD5185028AA2FBA003F3D2A /* UIViewController+Convenience.swift in Sources */, diff --git a/Sources/BottomSheet/Core/BottomSheetConfiguration.swift b/Sources/BottomSheet/Core/BottomSheetConfiguration.swift index b42b9db..c253d6f 100644 --- a/Sources/BottomSheet/Core/BottomSheetConfiguration.swift +++ b/Sources/BottomSheet/Core/BottomSheetConfiguration.swift @@ -27,10 +27,12 @@ public struct BottomSheetConfiguration { public struct ShadowConfiguration { public let backgroundColor: UIColor public let blur: UIBlurEffect.Style? + public let intensity: CGFloat - public init(backgroundColor: UIColor, blur: UIBlurEffect.Style? = nil) { + public init(backgroundColor: UIColor, blur: UIBlurEffect.Style? = nil, intensity: CGFloat? = nil) { self.backgroundColor = backgroundColor self.blur = blur + self.intensity = intensity ?? 0.1 } public static let `default` = ShadowConfiguration(backgroundColor: UIColor.black.withAlphaComponent(0.6)) diff --git a/Sources/BottomSheet/Core/Extensions/CustomIntensityVisualEffectView.swift b/Sources/BottomSheet/Core/Extensions/CustomIntensityVisualEffectView.swift new file mode 100644 index 0000000..2204792 --- /dev/null +++ b/Sources/BottomSheet/Core/Extensions/CustomIntensityVisualEffectView.swift @@ -0,0 +1,42 @@ +// +// CustomIntensityVisualEffectView.swift +// BottomSheet +// +// Created by Pierre Boulc'h on 06/02/2023. +// Copyright © 2023 Joom. All rights reserved. +// + +import UIKit + +final class CustomIntensityVisualEffectView: UIVisualEffectView { + /// Create visual effect view with given effect and its intensity + /// + /// - Parameters: + /// - effect: visual effect, eg UIBlurEffect(style: .dark) + /// - intensity: custom intensity from 0.0 (no effect) to 1.0 (full effect) using linear scale + init(effect: UIVisualEffect, intensity: CGFloat) { + theEffect = effect + customIntensity = intensity + super.init(effect: nil) + } + + required init?(coder aDecoder: NSCoder) { nil } + + deinit { + animator?.stopAnimation(true) + } + + override func draw(_ rect: CGRect) { + super.draw(rect) + effect = nil + animator?.stopAnimation(true) + animator = UIViewPropertyAnimator(duration: 1, curve: .linear) { [unowned self] in + self.effect = theEffect + } + animator?.fractionComplete = customIntensity + } + + private let theEffect: UIVisualEffect + private let customIntensity: CGFloat + private var animator: UIViewPropertyAnimator? +} diff --git a/Sources/BottomSheet/Core/Presentation/BottomSheetPresentationController.swift b/Sources/BottomSheet/Core/Presentation/BottomSheetPresentationController.swift index a74fbab..637cf15 100644 --- a/Sources/BottomSheet/Core/Presentation/BottomSheetPresentationController.swift +++ b/Sources/BottomSheet/Core/Presentation/BottomSheetPresentationController.swift @@ -286,7 +286,7 @@ public final class BottomSheetPresentationController: UIPresentationController { private func addShadow(containerView: UIView) { var shadingView = UIView() if let blur = configuration.shadowConfiguration.blur { - shadingView = UIVisualEffectView(effect: UIBlurEffect(style: blur)) + shadingView = CustomIntensityVisualEffectView.init(effect: UIBlurEffect.init(style: blur), intensity: configuration.shadowConfiguration.intensity) } shadingView.backgroundColor = configuration.shadowConfiguration.backgroundColor