Skip to content
43 changes: 43 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ For showing a simple text message:
SPAlert.present(message: "Text!")
```

For showing a loading view without customization:

```swift
let alert = SPAlert.presentLoader(message: "Loading...")
alert.dismis() // To dismiss the alert
```

For showing a loading view without customization but with timeout:

```swift
let _ = SPAlert.presentLoader(message: "Loading...", timeout: 5)
```

## Usage

If you require deep customization, this section will show you what you can do.
Expand Down Expand Up @@ -183,6 +196,36 @@ You can change the corner radius by setting the `cornerRadius` property:
alertView.layer.cornerRadius = 40
```

### Loading View

The loading view can be a square or it can calculate the size needed

```swift
let loadingView = SPAlertView(loadingMessage = "Loading...")
loadingView.makeLoadingViewRectangular = true // this is the default value
loadingView.present()
```

### Background Blur

Background blur is available to the loading view as default or to the other alerts by adding to the configuration

```swift
let alertView = SPAlertView(title: "Added to Library", message: nil, preset: SPAlertPreset.done)
alertView.blurBackground = true // The default value for alerts is false
alertView.present()
```

To change the blur background

```swift
let loadingView = SPAlertView(loadingMessage = "Loading...")
loadingView.backgroundBlurRadius = 10 // The default value is 5
loadingView.present()
```

**Note:** This only blurs the views under the alert or the loading view, and not the alert or loading view it self.

## Сooperation

This project is free to use, but developing it takes time. Contributing to this project is a huge help. Here is list of tasks that need to be done:
Expand Down
4 changes: 4 additions & 0 deletions SPAlert.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3F57AB06243F61B700C1F9AF /* SPAlertBlurBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F57AB05243F61B700C1F9AF /* SPAlertBlurBackground.swift */; };
706F894A2383277500ECF5D1 /* SPAlertIconErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 706F89492383277500ECF5D1 /* SPAlertIconErrorView.swift */; };
F40FB62A239ADF940090BA57 /* SPAlertIconQuestionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F40FB629239ADF940090BA57 /* SPAlertIconQuestionView.swift */; };
F41204E92382AC9B009C2AC7 /* SPAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F41204DF2382AC9B009C2AC7 /* SPAlertView.swift */; };
Expand Down Expand Up @@ -70,6 +71,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
3F57AB05243F61B700C1F9AF /* SPAlertBlurBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPAlertBlurBackground.swift; sourceTree = "<group>"; };
706F89492383277500ECF5D1 /* SPAlertIconErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPAlertIconErrorView.swift; sourceTree = "<group>"; };
F40FB629239ADF940090BA57 /* SPAlertIconQuestionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPAlertIconQuestionView.swift; sourceTree = "<group>"; };
F41204D02382ABE9009C2AC7 /* SPAlert.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SPAlert.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -285,6 +287,7 @@
children = (
F41204E12382AC9B009C2AC7 /* Icons */,
F41204DF2382AC9B009C2AC7 /* SPAlertView.swift */,
3F57AB05243F61B700C1F9AF /* SPAlertBlurBackground.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -411,6 +414,7 @@
files = (
F412052A2382AF64009C2AC7 /* SPAlertHaptic.swift in Sources */,
F41204EE2382AC9B009C2AC7 /* SPAlert.swift in Sources */,
3F57AB06243F61B700C1F9AF /* SPAlertBlurBackground.swift in Sources */,
F40FB62A239ADF940090BA57 /* SPAlertIconQuestionView.swift in Sources */,
F4E4459D2390524A0067FF5C /* SPAlertIconExclamationView.swift in Sources */,
F459720223A841C00092CE7C /* SPAlertIconRotateView.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Source/SPAlert/SPAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,20 @@ public enum SPAlert {
alertView.haptic = haptic
alertView.present()
}

/**
Present the loading view

- parameter timeout: If not passed the view will not be dismissed
- returns: SPAlertView so the loading view can be dismissed and or reused
*/
public static func presentLoading(message: String, timeout: Double? = nil) -> SPAlertView {
let alertView = SPAlertView(loadingMessage: message)
alertView.width = 125
alertView.dismissByTap = false
alertView.disableUserInteractionWhenPresenting = true
alertView.duration = timeout
return alertView
}

}
66 changes: 66 additions & 0 deletions Source/SPAlert/Views/SPAlertBlurBackground.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// SPAlertBlurBackground.swift
// SPAlert iOS
//
// Created by Pedro Cavaleiro on 09/04/2020.
// Copyright © 2020 Ivan Vorobei. All rights reserved.
//

import Foundation
import UIKit

// This class is the container for the blur view
class SPAlertBlurBackground: UIView {

init(keyWindow: UIView, withRadius blurRadius: CGFloat = 5) {
super.init(frame: keyWindow.bounds)
self.backgroundColor = .clear
self.frame = keyWindow.bounds

let blurView = BlurView(withRadius: blurRadius)
blurView.frame = keyWindow.bounds

addSubview(blurView)
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}

// This class was created by Collin Hundley on 1/15/16
// The native method using the UIBlurEffect does not allow to set the blur radius
fileprivate class BlurView: UIVisualEffectView {

private let blurEffect: UIBlurEffect
public var blurRadius: CGFloat {
return blurEffect.value(forKeyPath: "blurRadius") as! CGFloat
}

public convenience init() {
self.init(withRadius: 0)
}

public init(withRadius radius: CGFloat) {
let blurClass: AnyObject.Type = NSClassFromString("_UICustomBlurEffect")!
let blurObject: NSObject.Type = blurClass as! NSObject.Type
self.blurEffect = blurObject.init() as! UIBlurEffect
self.blurEffect.setValue(1.0, forKeyPath: "scale")
self.blurEffect.setValue(radius, forKeyPath: "blurRadius")
super.init(effect: radius == 0 ? nil : self.blurEffect)
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public func setBlurRadius(radius: CGFloat) {
guard radius != blurRadius else {
return
}
blurEffect.setValue(radius, forKeyPath: "blurRadius")
self.effect = blurEffect
}

}

}
Loading