![CI Status](http://img.shields.io/travis/Masahiko Tsujita/CRToastSwift.svg?style=flat)
A wrapper library of CRToast totally redesigned for Swift 2.
- Awesome UX based on CRToast
- Carefully considered, highly structured and Swiftish API
- Strongly typed properties
- Flowable event handlings by method chaining and trailing closures
- Easy customization for your applications
- Xcode 7 and later
- CRToast 0.0.9 and later
CRToastSwift is available through Carthage. To install,
github "masahiko24/CRToastSwift"
CRToastSwift also is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "CRToastSwift"
First, define your notification type conforming NotificationType
protocol.
It requires two text properties:
public protocol NotificationType {
var text: String { get }
var subtext: String? { get }
}
There are two predefined types conforming to NotificationType
- Notification
and String
.
Notification
is a simple notification with two required text properties with given parameters on initialization.
String
also conforms to the protocol. It provides itself for text
and always nil
for subtext
.
If these types are enough for you, you do not have to define custom notification types.
PresentationContextType
is a context which notifications are presented.
You can customize apparances and behaviors of notifications as follows:
class MyContext :PresentationContextType {
func attributesForNotification(notification: Notification) -> NotificationAttributeCollection {
var attributes = NotificationAttributeCollection()
attributes.image = UIImage(named: "Error")
attributes.backgroundColor = .redColor()
attributes.showsStatusBar = true
return attributes
}
}
You can present notifications by calling Presenter.present()
and handle events using method chaining:
Presenter.present("Hello, world", context: context) { (notification, _) in
// Handling presentation
print("\(notification) was presented.")
} .on(.Tap) { (notification, _, dismisser) in
// Handling users tap
print("\(notification) was tapped.")
// You can dismiss the notification on user interaction using dismisser
dismisser.dismiss { notification in
// Handling dismissal by specific user interaction
print("\(notification) was dismissed by tap.")
}
} .onDismissal { notification in
// Handling dismissal
print("\(notification) was dismissed.")
}
You can specify animations on presentation as follows:
Presenter.present(..., animation: .Gravity) ...
animation
can take an instance of Animation
.
There are three predefined animation values: Linear
, Spring
, and Gravity
.
public struct Animation {
public static let Linear: Animation
public static let Spring: Animation
public static let Gravity: Animation
}
You can also specify how long the notification will be presented:
Presenter.present(..., duration: 5.0) ...
You can specify nil
for duration
. Then the notification will not be dismissed automatically, so you have to dismiss the notification manually using dismisser
given as a parameter of handlers of presentation or user interactions.
CRToastSwift is available under the MIT license. See the LICENSE file for more info.