MPGNotifications 是一个 iOS 控件,它允许您显示与您的需求完全可自定义的交互式通知。
通过 Cocoapods 获取: pod 'MPGNotification', '~> 1.2'
MPGNotification
对象是 UIView
对象。它们使用自包含逻辑显示在“所有内容”之上,该逻辑获取顶部窗口的级别(由 UIApplciation
报告)。
只需初始化它们,配置它们,然后显示即可!就是这样!
初始化 MPGNotification
对象很简单 - 只需使用 init! 或者 new!
MPGNotification *notification = [[MPGNotification alloc] init];
MPGNotification *anotherNotification = [MPGNotification new];
您还可以使用以下便捷方法以已设置许多“基本”可视化属性的方式初始化通知
MPGNotification *easyNotification =
[MPGNotification notificationWithTitle:@"Greetings!"
subtitle:@"Did you know we have Notifications now?"
backgroundColor:[UIColor redColor]
iconImage:[UIImage imageNamed:@"radical"]];
配置好通知后,将其显示在屏幕上很容易!因为 MPGNotification
发现其自定义视图和位置,只需调用 show
[notification show];
但是,单靠显示通知可能有些局限。如果有人点击通知,您想要执行什么操作呢?
如果用户点击通知的背景或您配置的按钮之一,您可以通过两种方式执行操作。您可以将 buttonHandler
属性设置为以下内容
notification.buttonHandler = ^(MPGNotification *notification, NSInteger buttonIndex) {
if (buttonIndex == notification.firstButton.tag) {
NSLog("User tapped the only button on-screen!");
}
};
[notification show];
..或您可以使用以下便捷方法简单地显示通知
// easyNotification.buttonHandler == nil
[easyNotification showWithButtonHandler:^(MPGNotification *notification, NSInteger buttonIndex) {
if (buttonIndex == notification.backgroundView.tag) {
NSLog("User tapped the background of the Notification!");
}
}];
以下按钮配置可用
typedef NS_ENUM(NSInteger, MPGNotificationButtonConfigration) {
MPGNotificationButtonConfigrationZeroButtons = 0,
MPGNotificationButtonConfigrationOneButton,
MPGNotificationButtonConfigrationTwoButton,
MPGNotificationButtonConfigrationCloseButton
};
用于 UI 的按钮会根据以下配置进行适配
switch (self.buttonConfiguration) {
case MPGNotificationButtonConfigrationZeroButtons:
// self.firstButton, self.secondButton, and self.closeButton == nil
break;
case MPGNotificationButtonConfigrationOneButton:
// self.firstButton != nil
// self.secondButton and self.closeButton == nil
break;
case MPGNotificationButtonConfigrationTwoButton:
// self.firstButton and self.secondButton != nil
// self.closeButton == nil
break;
case MPGNotificationButtonConfigrationCloseButton:
// self.closeButton != nil
// self.firstButton and self.secondButton == nil
break;
}
// self.backgroudnView is unrelated to self.buttonConfiguration, and is always != nil, but does not always receive touches
必须在调用 show
或 showWithButtonHandler:
之前设置所有属性。以下属性和 'setter 方法' 可用
// Properties used for basic styling
@property (nonatomic, strong) NSString *title; // required
@property (nonatomic, strong) NSString *subtitle; // optional
@property (nonatomic, strong) UIImage *iconImage; // optional
@property (nonatomic, strong) UIColor *backgroundColor; // optional
// Allows actions and dismissal when the background of the Notification is tapped.
// Default: YES
@property (nonatomic) BOOL backgroundTapsEnabled;
// Allows 'swipe to dismiss' action on the Notification, similar to iOS Push Notifications.
// Default: YES
@property (nonatomic) BOOL swipeToDismissEnabled;
// Allows full-screen messages on iPad. Defaults to NO, similar to iOS Push Notifications.
@property (nonatomic) BOOL fullWidthMessages;
// To set the title color of the notification.
// Default: [UIColor whiteColor]
@property (nonatomic, strong) UIColor *titleColor;
// To set the subtitle color of the notification.
// Default: [UIColor whiteColor]
@property (nonatomic, strong) UIColor *subtitleColor;
// Set this to any positive value to automatically dismiss the Notification after the given duration.
// Default: 0.0
@property (nonatomic) NSTimeInterval duration;
// Used to specify the type of animation that the notification should use to show and dismiss.
// Default: MPGNotificationAnimationTypeLinear
@property (nonatomic) MPGNotificationAnimationType animationType;
// Sets the button handler block directly; is also be set indirectly by calling showWithButtonHandler:
// Default: nil
@property (nonatomic, strong) MPGNotificationButtonHandler buttonHandler;
// Sets a dismiss hanlder block that is called when the Notification is dismissed
// Default: nil
@property (nonatomic, copy) MPGNotificationDismissHandler dismissHandler;
// Read-only value of the current button configuration
// Default: MPGNotificationButtonConfigrationZeroButtons
@property (nonatomic, readonly) MPGNotificationButtonConfigration buttonConfiguration;
// Sets the configuration and titles for the Notification's visible buttons. The number of buttonTitles supplied must match the configuration.
- (void)setButtonConfiguration:(MPGNotificationButtonConfigration)configuration withButtonTitles:(NSArray *)buttonTitles;
// construct notification
UIImage *chatImage = [[UIImage imageNamed:@"icon-chat"] colorImageWhite];
MPGNotification *notification =
[MPGNotification notificationWithTitle:self.chat.playerName
subtitle:self.chat.message
backgroundColor:[UIColor customChatColor]
iconImage:chatImage];
// auto-dismiss after desired time in seconds
notification.duration = 6.0;
// button & touch handling
notification.backgroundTapsEnabled = YES;
[notification setButtonConfiguration:MPGNotificationButtonConfigrationOneButton withButtonTitles:@[@"Reply"]];
// set animation type
notification.animationType = MPGNotificationAnimationTypeDrop;
// show the notification and handle button taps
// (self.firstButton is the Reply button, self.backgroundView is the background tap)
[notification showWithButtonHandler:^(MPGNotification *notification, NSInteger buttonIndex) {
if (buttonIndex == notification.firstButton.tag ||
buttonIndex == notification.backgroundView.tag) {
[self scrollToChat];
}
}];