diff --git a/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate b/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate index 981e51c..d42a160 100644 Binary files a/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate and b/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Example Apps/iOS Example/Scenes/PresetsController.swift b/Example Apps/iOS Example/Scenes/PresetsController.swift index 880dddc..dc7800a 100644 --- a/Example Apps/iOS Example/Scenes/PresetsController.swift +++ b/Example Apps/iOS Example/Scenes/PresetsController.swift @@ -45,6 +45,10 @@ class PresetsController: SPDiffableTableController { currentPreset = presets.first! setCellProviders(SPDiffableTableCellProviders.default, sections: content) + let segmentedControl = UISegmentedControl(items: ["Top", "Bottom"]) + navigationItem.titleView = segmentedControl + segmentedControl.selectedSegmentIndex = 0 + navigationController?.isToolbarHidden = false toolbarItems = [ .init(image: .system("chevron.down.circle"), primaryAction: .init(handler: { (action) in @@ -60,11 +64,14 @@ class PresetsController: SPDiffableTableController { })), .init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), .init(systemItem: .play, primaryAction: .init(handler: { [weak self] (action) in - guard let preset = self?.currentPreset else { return } + guard let self = self else { return } + guard let preset = self.currentPreset else { return } + guard let segmentControl = self.navigationItem.titleView as? UISegmentedControl else { return } + let presentPosition = segmentControl.selectedSegmentIndex == 0 ? SPIndicatorPresentSide.top : SPIndicatorPresentSide.botton if let iconPreset = preset.preset { - SPIndicator.present(title: preset.title, message: preset.message, preset: iconPreset, completion: nil) + SPIndicator.present(title: preset.title, message: preset.message, preset: iconPreset, from: presentPosition, completion: nil) } else { - SPIndicator.present(title: preset.title, message: preset.message, haptic: .success) + SPIndicator.present(title: preset.title, message: preset.message, haptic: .success, from: presentPosition) } }), menu: nil), diff --git a/README.md b/README.md index b483433..70e3277 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ If you like the project, don't forget to `put star ★` and follow me on GitHub: - [Layout](#layout) - [Dismiss by Drag](#dismiss-by-drag) - [Haptic](#haptic) + - [Present Side](#present-side) - [Other Projects](#other-projects) - [Russian Community](#russian-community) @@ -59,7 +60,7 @@ If you prefer not to use any of dependency managers, you can integrate `SPIndica ## Quick Start -For best experience, I recommend presenting alerts by calling the class functions `SPIndicator`. These functions are updated regularly and show the alerts as Apple way: +For best experience, I recommend presenting indicator by calling the class functions `SPIndicator`. These functions are updated regularly and show the indicator as Apple way: ```swift SPIndicator.present(title: "Error", message: "Try Again", preset: .error) @@ -85,7 +86,7 @@ You can provide message optional too. ### Duration -For change duration of present time, create alert view and call `present` method with custom duration: +For change duration of present time, create indicator view and call `present` method with custom duration: ```swift let indicatorView = SPIndicatorView(title: "Complete", preset: .done) @@ -103,7 +104,7 @@ indicatorView.layout.margins.top = 12 ### Dismiss by Drag -By default allow drag alert for hide. While alert is dragging, dismiss not work. This can be disabled: +By default allow drag indicator for hide. While indicator is dragging, dismiss not work. This can be disabled: ```swift indicatorView.dismissByDrag = false @@ -114,11 +115,24 @@ indicatorView.dismissByDrag = false For manage haptic, you shoud pass it in present method: ```swift -alertView.present(duration: 1.5, haptic: .success, completion: nil) +indicatorView.present(duration: 1.5, haptic: .success, completion: nil) ``` You can remove duration and completion, its have default values. +### Present Side + +If you need present from special side, use this: + +```swift +SPIndicator.present(title: "Error", message: "Try Again", preset: .error, from: .bottom) + +// or with custom view + +indicatorView.presentSide = .bottom +``` +In last cases indicator will appear from bottom and attached to bottom. + ## Other Projects #### [SPPermissions](https://github.com/ivanvorobei/SPPermissions) diff --git a/SPIndicator.podspec b/SPIndicator.podspec index 7496f91..b6e741f 100644 --- a/SPIndicator.podspec +++ b/SPIndicator.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'SPIndicator' - s.version = '1.0.6' + s.version = '1.1.0' s.summary = 'Floating indicator, mimicrate to indicator which appear when silent mode turn on / off. Support large texts.' s.homepage = 'https://github.com/ivanvorobei/SPIndicator' s.source = { :git => 'https://github.com/ivanvorobei/SPIndicator.git', :tag => s.version } diff --git a/Sources/SPIndicator/Models/SPIndicatorPresentSide.swift b/Sources/SPIndicator/Models/SPIndicatorPresentSide.swift new file mode 100644 index 0000000..1dfb74f --- /dev/null +++ b/Sources/SPIndicator/Models/SPIndicatorPresentSide.swift @@ -0,0 +1,31 @@ +// The MIT License (MIT) +// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.by) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import Foundation + +/** + SPIndicator: Cases from preset styles. + */ +public enum SPIndicatorPresentSide { + + case top + case botton +} diff --git a/Sources/SPIndicator/SPIndicator.swift b/Sources/SPIndicator/SPIndicator.swift index 0b39c7f..27b174c 100644 --- a/Sources/SPIndicator/SPIndicator.swift +++ b/Sources/SPIndicator/SPIndicator.swift @@ -32,10 +32,12 @@ public enum SPIndicator { - parameter message: Subtitle text in alert. Optional. - parameter preset: Icon ready-use style or custom image. - parameter haptic: Haptic response with present. Default is `.success`. + - parameter presentSide: Choose from side appear indicator. - parameter completion: Will call with dismiss alert. */ - public static func present(title: String, message: String? = nil, preset: SPIndicatorIconPreset, haptic: SPIndicatorHaptic, completion: (() -> Void)? = nil) { + public static func present(title: String, message: String? = nil, preset: SPIndicatorIconPreset, haptic: SPIndicatorHaptic, from presentSide: SPIndicatorPresentSide = .top, completion: (() -> Void)? = nil) { let alertView = SPIndicatorView(title: title, message: message, preset: preset) + alertView.presentSide = presentSide alertView.present(haptic: haptic, completion: completion) } @@ -45,10 +47,12 @@ public enum SPIndicator { - parameter title: Title text in alert. - parameter message: Subtitle text in alert. Optional. - parameter preset: Icon ready-use style or custom image. + - parameter presentSide: Choose from side appear indicator. Default is `.top`. - parameter completion: Will call with dismiss alert. */ - public static func present(title: String, message: String? = nil, preset: SPIndicatorIconPreset, completion: (() -> Void)? = nil) { + public static func present(title: String, message: String? = nil, preset: SPIndicatorIconPreset, from presentSide: SPIndicatorPresentSide = .top, completion: (() -> Void)? = nil) { let alertView = SPIndicatorView(title: title, message: message, preset: preset) + alertView.presentSide = presentSide let haptic = preset.getHaptic() alertView.present(haptic: haptic, completion: completion) } @@ -58,10 +62,12 @@ public enum SPIndicator { - parameter message: Title text. - parameter haptic: Haptic response with present. Default is `.success`. + - parameter presentSide: Choose from side appear indicator. Default is `.top`. - parameter completion: Will call with dismiss alert. */ - public static func present(title: String, message: String? = nil, haptic: SPIndicatorHaptic, completion: (() -> Void)? = nil) { + public static func present(title: String, message: String? = nil, haptic: SPIndicatorHaptic, from presentSide: SPIndicatorPresentSide = .top, completion: (() -> Void)? = nil) { let alertView = SPIndicatorView(title: title, message: message) + alertView.presentSide = presentSide alertView.present(haptic: haptic, completion: completion) } diff --git a/Sources/SPIndicator/SPIndicatorView.swift b/Sources/SPIndicator/SPIndicatorView.swift index 72a3ded..ea5062e 100644 --- a/Sources/SPIndicator/SPIndicatorView.swift +++ b/Sources/SPIndicator/SPIndicatorView.swift @@ -39,6 +39,12 @@ open class SPIndicatorView: UIView { // MARK: - Properties + /** + SPIndicator: Change it for set `top` or `bottom` present side. + Shoud be change before present, instead of no effect. + */ + open var presentSide: SPIndicatorPresentSide = .top + open var dismissByDrag: Bool = true { didSet { setGester() @@ -97,6 +103,7 @@ open class SPIndicatorView: UIView { } public required init?(coder aDecoder: NSCoder) { + self.presentSide = .top super.init(coder: aDecoder) commonInit() } @@ -194,14 +201,14 @@ open class SPIndicatorView: UIView { sizeToFit() layoutSubviews() center.x = window.frame.midX - toPresentPosition(.prepare) + toPresentPosition(.prepare(presentSide)) // Present isHidden = false haptic.impact() UIView.animate(withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseOut], animations: { - self.toPresentPosition(.visible) + self.toPresentPosition(.visible(self.presentSide)) }, completion: { finished in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) { if self.gesterIsDragging { @@ -221,7 +228,7 @@ open class SPIndicatorView: UIView { @objc open func dismiss() { UIView.animate(withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseIn], animations: { - self.toPresentPosition(.prepare) + self.toPresentPosition(.prepare(self.presentSide)) }, completion: { finished in self.removeFromSuperview() self.completion?() @@ -245,7 +252,7 @@ open class SPIndicatorView: UIView { return min(maxmiumYTranslationByGester, translation.y.squareRoot()) } }() - toPresentPosition(.fromVisible(newTranslation)) + toPresentPosition(.fromVisible(newTranslation, from: (presentSide))) } if gestureRecognizer.state == .ended { @@ -253,13 +260,13 @@ open class SPIndicatorView: UIView { UIView.animate(withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseIn], animations: { if self.whenGesterEndShoudHide { - self.toPresentPosition(.prepare) + self.toPresentPosition(.prepare(self.presentSide)) } else { let translation = gestureRecognizer.translation(in: self) if translation.y < self.minimumYTranslationForHideByGester { - self.toPresentPosition(.prepare) + self.toPresentPosition(.prepare(self.presentSide)) } else { - self.toPresentPosition(.visible) + self.toPresentPosition(.visible(self.presentSide)) } } }, completion: nil) @@ -268,21 +275,45 @@ open class SPIndicatorView: UIView { private func toPresentPosition(_ position: PresentPosition) { switch position { - case .prepare: - let position = -((UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0) + 50) - transform = CGAffineTransform.identity.translatedBy(x: 0, y: position) - case .visible: - transform = visibleTranform - case .fromVisible(let value): - transform = visibleTranform.translatedBy(x: 0, y: value) + case .prepare(let presentSide): + transform = getPrepareTransform(from: presentSide) + case .visible(let presentSide): + transform = getVisibleTransform(from: presentSide) + case .fromVisible(let translation, let presentSide): + transform = getVisibleTransform(from: presentSide).translatedBy(x: 0, y: translation) } } - private var visibleTranform: CGAffineTransform { - var topSafeAreaInsets = (UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0) - if topSafeAreaInsets < 20 { topSafeAreaInsets = 20 } - let position = topSafeAreaInsets - 3 - return CGAffineTransform.identity.translatedBy(x: 0, y: position) + private func getPrepareTransform(from side: SPIndicatorPresentSide) -> CGAffineTransform { + guard let window = UIApplication.shared.windows.first else { return .identity } + switch side { + case .top: + let topInset = window.safeAreaInsets.top + let position = -(topInset + 50) + return CGAffineTransform.identity.translatedBy(x: 0, y: position) + case .botton: + let height = window.frame.height + let bottonInset = window.safeAreaInsets.bottom + let position = height + bottonInset + 50 + return CGAffineTransform.identity.translatedBy(x: 0, y: position) + } + } + + private func getVisibleTransform(from side: SPIndicatorPresentSide) -> CGAffineTransform { + guard let window = UIApplication.shared.windows.first else { return .identity } + switch side { + case .top: + var topSafeAreaInsets = window.safeAreaInsets.top + if topSafeAreaInsets < 20 { topSafeAreaInsets = 20 } + let position = topSafeAreaInsets - 3 + return CGAffineTransform.identity.translatedBy(x: 0, y: position) + case .botton: + let height = window.frame.height + var bottomSafeAreaInsets = window.safeAreaInsets.top + if bottomSafeAreaInsets < 20 { bottomSafeAreaInsets = 20 } + let position = height - bottomSafeAreaInsets - 3 - frame.height + return CGAffineTransform.identity.translatedBy(x: 0, y: position) + } } // MARK: - Layout @@ -475,9 +506,9 @@ open class SPIndicatorView: UIView { enum PresentPosition { - case prepare - case visible - case fromVisible(_ translation: CGFloat) + case prepare(_ from: SPIndicatorPresentSide) + case visible(_ from: SPIndicatorPresentSide) + case fromVisible(_ translation: CGFloat, from: SPIndicatorPresentSide) } enum LayoutGrid {