NewNotifier.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // NewNotifier.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/26.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "NewNotifier.h"
  9. #import <AudioToolbox/AudioToolbox.h>
  10. @interface NewNotifier()
  11. @property (nonatomic, strong) NewNotifierBar *notifierBar;
  12. @property (nonatomic, strong) UIImage *defaultIcon;
  13. @property (nonatomic, strong) NSString *appName;
  14. @end
  15. @implementation NewNotifier
  16. + (NewNotifier*)shareInstance {
  17. static dispatch_once_t onceToken;
  18. static NewNotifier *notifier;
  19. dispatch_once(&onceToken, ^ {
  20. notifier = [[self alloc] init];
  21. });
  22. return notifier;
  23. }
  24. - (instancetype)init
  25. {
  26. self = [super init];
  27. if (self) {
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifierOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
  29. }
  30. return self;
  31. }
  32. #pragma --mark getter
  33. - (NSString*)appName{
  34. if (!_appName) {
  35. _appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]?:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
  36. }
  37. return _appName;
  38. }
  39. - (UIImage*)defaultIcon{
  40. if (!_defaultIcon) {
  41. _defaultIcon = [self loadPlistIcon] ?:[UIImage imageNamed:@"AppIcon"] ?:[UIImage imageNamed:@"AppIcon-1"] ?:[UIImage imageNamed:@"AppIcon-2"] ?:[UIImage imageNamed:@"AppIcon-3"] ?:[UIImage imageNamed:@"icon"];
  42. }
  43. return _defaultIcon;
  44. }
  45. - (NewNotifierBar*)notifierBar{
  46. if (!_notifierBar) {
  47. _notifierBar = [[NewNotifierBar alloc] init];
  48. CGRect frame = _notifierBar.frame;
  49. frame.origin.y = -frame.size.height;
  50. _notifierBar.frame = frame;
  51. }
  52. return _notifierBar;
  53. }
  54. + (void)handleClickAction:(NewNotifierBarClickBlock)notifierBarClickBlock{
  55. [[self shareInstance].notifierBar handleClickAction:notifierBarClickBlock];
  56. }
  57. #pragma --mark class method
  58. + (NewNotifierBar*)showNotiferRemain:(NSString*)note{
  59. return [NewNotifier showNotiferRemain:note name:nil];
  60. }
  61. + (NewNotifierBar*)showNotiferRemain:(NSString*)note
  62. name:(NSString*)appName{
  63. return [NewNotifier showNotifer:note name:appName icon:nil dismissAfter:-1];
  64. }
  65. + (NewNotifierBar*)showNotifer:(NSString*)note{
  66. return [NewNotifier showNotifer:note dismissAfter:4];
  67. }
  68. + (NewNotifierBar*)showNotifer:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
  69. return [NewNotifier showNotifer:note name:appName icon:appIcon dismissAfter:2];
  70. }
  71. + (NewNotifierBar*)showNotifer:(NSString *)note
  72. dismissAfter:(NSTimeInterval)delay{
  73. return [self showNotifer:note name:nil icon:nil dismissAfter:delay];
  74. }
  75. + (NewNotifierBar*)showNotifer:(NSString*)note
  76. name:(NSString*)appName
  77. icon:(UIImage*)appIcon
  78. dismissAfter:(NSTimeInterval)delay{
  79. NewNotifierBar *bar = [[self shareInstance] showNotifer:note
  80. name:appName?:[self shareInstance].appName
  81. icon:appIcon?:[self shareInstance].defaultIcon];
  82. [self dismissAfter:delay];
  83. return bar;
  84. }
  85. + (void)dismiss{
  86. [[self shareInstance] dismiss];
  87. }
  88. + (void)dismissAfter:(NSTimeInterval)delay;
  89. {
  90. if(delay<0)
  91. {
  92. [NSObject cancelPreviousPerformRequestsWithTarget:[self shareInstance] selector:@selector(dismiss) object:nil];
  93. }else
  94. {
  95. [[self shareInstance] performSelector:@selector(dismiss) withObject:nil afterDelay:delay];
  96. }
  97. }
  98. #pragma --instance method
  99. - (NewNotifierBar*)showNotifer:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
  100. [self.notifierBar.layer removeAllAnimations];
  101. self.notifierBar.userInteractionEnabled = YES;
  102. [self.notifierBar removeFromSuperview];
  103. self.notifierBar = nil;
  104. //播放系统的通知声音类型 如果使用自定义的 则请关闭这个 不然自定义的声音会失效
  105. AudioServicesPlaySystemSound(1007);
  106. [self.notifierBar show:note name:appName icon:appIcon];
  107. [UIView animateWithDuration:(0.4) animations:^{
  108. self.notifierBar.alpha = 0.8;
  109. CGRect frame = _notifierBar.frame;
  110. frame.origin.y = 0.;
  111. _notifierBar.frame = frame;
  112. }];
  113. return self.notifierBar;
  114. }
  115. - (void)dismiss
  116. {
  117. [self dismissWithAnimation:YES];
  118. }
  119. - (void)dismissWithAnimation:(BOOL)animated{
  120. [[NSRunLoop currentRunLoop] cancelPerformSelector:@selector(dismiss) target:self argument:nil];
  121. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismiss) object:nil];
  122. if(animated){
  123. [UIView animateWithDuration:0.4 animations:^{
  124. CGRect frame = _notifierBar.frame;
  125. frame.origin.y = -frame.size.height;
  126. _notifierBar.frame = frame;
  127. } completion:^(BOOL finished) {
  128. self.notifierBar.userInteractionEnabled = NO;
  129. _notifierBar.hidden = YES;
  130. }];
  131. }else{
  132. _notifierBar.hidden = YES;
  133. }
  134. }
  135. - (void)notifierOrientationChange:(NSNotification *)notification
  136. {
  137. [self dismissWithAnimation:NO];
  138. }
  139. #pragma --mark helper
  140. - (UIImage*)loadPlistIcon{
  141. NSString *iconString = @"Icon.png";
  142. NSArray *icons = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIconFiles"];
  143. if (!icons) {
  144. iconString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIconFile"];
  145. }
  146. else
  147. {
  148. iconString = icons [0];
  149. }
  150. return [UIImage imageNamed:iconString];
  151. }
  152. @end