BSAlertView.m.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // BSAlertView.m
  3. // Unionpay
  4. //
  5. // Created by Qing Xiubin on 13-8-7.
  6. // Copyright (c) 2013年 成都中信联通科技有限公司. All rights reserved.
  7. //
  8. #if !__has_feature(objc_arc)
  9. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  10. #endif
  11. #define ALERT_TIME_DURATION 0.3f
  12. #define ALERT_ALPHA_HIDE 0.0f
  13. #define ALERT_ALPHA_SHOW 1.0f
  14. #define ALERT_COLOR_HIDE [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]
  15. #define ALERT_COLOR_SHOW [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f]
  16. #import "BSAlertView.h"
  17. #import "IQKeyboardManager.h"
  18. @interface AlertController : UIViewController
  19. @end
  20. @implementation AlertController
  21. - (UIStatusBarStyle)preferredStatusBarStyle{
  22. return UIStatusBarStyleDefault;
  23. }
  24. - (BOOL)prefersStatusBarHidden{
  25. return NO;
  26. }
  27. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation{
  28. return UIStatusBarAnimationFade;
  29. }
  30. -(BOOL)shouldAutorotate
  31. {
  32. return YES;
  33. }
  34. -(UIInterfaceOrientationMask)supportedInterfaceOrientations
  35. {
  36. return UIInterfaceOrientationMaskPortrait;
  37. }
  38. @end
  39. @interface BSAlertView ()
  40. @property (assign, nonatomic) BOOL isShow;
  41. @property (strong, nonatomic) UIWindow *alertWindow;
  42. @property (strong, nonatomic) UIWindow *previousWindow;
  43. @end
  44. @implementation BSAlertView
  45. - (instancetype)init{
  46. return [super initWithFrame:CGRectZero];
  47. }
  48. - (instancetype)initWithFrame:(CGRect)frame{
  49. self = [super initWithFrame:frame];
  50. if (self) {
  51. [self awakeFromNib];
  52. }
  53. return self;
  54. }
  55. - (void)awakeFromNib{
  56. [super awakeFromNib];
  57. self.backgroundColor = [UIColor clearColor];
  58. self.autoresizingMask =
  59. UIViewAutoresizingFlexibleTopMargin|
  60. UIViewAutoresizingFlexibleBottomMargin|
  61. UIViewAutoresizingFlexibleLeftMargin|
  62. UIViewAutoresizingFlexibleRightMargin;
  63. //设置背景
  64. if (_bgImageView == nil) {
  65. _bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
  66. _bgImageView.autoresizingMask =
  67. UIViewAutoresizingFlexibleHeight|
  68. UIViewAutoresizingFlexibleWidth|
  69. UIViewAutoresizingFlexibleTopMargin|
  70. UIViewAutoresizingFlexibleBottomMargin|
  71. UIViewAutoresizingFlexibleLeftMargin|
  72. UIViewAutoresizingFlexibleRightMargin;
  73. [self insertSubview:_bgImageView atIndex:0];
  74. }
  75. _bgImageView.image = [UIImage stretchableImageNamed:@"common_middle_bg"];
  76. if (_gesture) {
  77. }
  78. }
  79. /**********************************************************************/
  80. #pragma mark - Public Methods
  81. /**********************************************************************/
  82. - (void)show{
  83. return [self showWithAnimation:kAlertAnimationBottom];
  84. }
  85. - (void)bgViewTapAction:(UITapGestureRecognizer *)sender{
  86. [self dismiss];
  87. }
  88. - (void)showWithAnimation:(AlertAnimation)animation{
  89. if (_isShow == YES) {
  90. return;
  91. }
  92. _isShow = YES;
  93. //监听键盘
  94. [self observeKeyboard];
  95. //设置Window
  96. [self setPreviousWindow:[[UIApplication sharedApplication] keyWindow]];
  97. _alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  98. [_alertWindow setWindowLevel:UIWindowLevelAlert];
  99. [_alertWindow setBackgroundColor:ALERT_COLOR_HIDE];
  100. AlertController *controller = [[AlertController alloc] init];
  101. [_alertWindow setRootViewController:controller];
  102. [_alertWindow.rootViewController.view addSubview:self];
  103. [_alertWindow makeKeyAndVisible];
  104. if (_gesture) {
  105. UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
  106. [bgButton setFrame:controller.view.bounds];
  107. [bgButton setBackgroundColor:[UIColor clearColor]];
  108. [bgButton addTarget:self action:@selector(bgViewTapAction:) forControlEvents:UIControlEventTouchUpInside];
  109. [controller.view insertSubview:bgButton belowSubview:self];
  110. }
  111. //弹窗动画
  112. switch (animation) {
  113. case kAlertAnimationTop:
  114. case kAlertAnimationBottom:
  115. case kAlertAnimationLeft:
  116. case kAlertAnimationRight:{
  117. [self setAlpha:ALERT_ALPHA_SHOW];
  118. [self setCenter:[self centerWithAnimation:animation]];
  119. [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  120. [self setCenter:[self centerWithAnimation:kAlertAnimationNone]];
  121. [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW];
  122. } completion:nil];
  123. }break;
  124. case kAlertAnimationFade:{
  125. [self setAlpha:ALERT_ALPHA_HIDE];
  126. [self setCenter:[self centerWithAnimation:animation]];
  127. [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  128. [self setAlpha:ALERT_ALPHA_SHOW];
  129. [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW];
  130. } completion:nil];
  131. }break;
  132. case kAlertAnimationPop:{
  133. [self setAlpha:ALERT_ALPHA_HIDE];
  134. [self setCenter:[self centerWithAnimation:animation]];
  135. [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  136. [self setAlpha:ALERT_ALPHA_SHOW];
  137. [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW];
  138. } completion:nil];
  139. CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
  140. scaleAnimation.removedOnCompletion = YES;
  141. scaleAnimation.duration = ALERT_TIME_DURATION;
  142. scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  143. scaleAnimation.values = @[@0.01f,@1.1f,@0.9f,@1.0f];
  144. [self.layer addAnimation:scaleAnimation forKey:nil];
  145. }break;
  146. case kAlertAnimationNone:
  147. default:{
  148. [self setAlpha:ALERT_ALPHA_SHOW];
  149. [self setCenter:[self centerWithAnimation:kAlertAnimationNone]];
  150. [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW];
  151. }break;
  152. }
  153. }
  154. - (void)dismiss{
  155. return [self dismissWithAnimation:kAlertAnimationFade];
  156. }
  157. - (void)dismissWithAnimation:(AlertAnimation)animation{
  158. if (_isShow == NO) {
  159. return;
  160. }
  161. _isShow = NO;
  162. //隐藏键盘
  163. [self unObserveKeyboard];
  164. [self endEditing:YES];
  165. //恢复Window
  166. [self.previousWindow makeKeyWindow];
  167. //弹窗动画
  168. switch (animation) {
  169. case kAlertAnimationTop:
  170. case kAlertAnimationBottom:
  171. case kAlertAnimationLeft:
  172. case kAlertAnimationRight:{
  173. [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  174. [self setCenter:[self centerWithAnimation:animation]];
  175. [self.alertWindow setBackgroundColor:ALERT_COLOR_HIDE];
  176. } completion:^(BOOL finished) {
  177. [self removeFromSuperview];
  178. }];
  179. }break;
  180. case kAlertAnimationFade:
  181. case kAlertAnimationPop:{
  182. [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  183. [self setAlpha:ALERT_ALPHA_HIDE];
  184. [self.alertWindow setBackgroundColor:ALERT_COLOR_HIDE];
  185. } completion:^(BOOL finished) {
  186. [self removeFromSuperview];
  187. }];
  188. }break;
  189. case kAlertAnimationNone:
  190. default:{
  191. [self setAlpha:ALERT_ALPHA_HIDE];
  192. [self.alertWindow setBackgroundColor:ALERT_COLOR_HIDE];
  193. [self removeFromSuperview];
  194. }break;
  195. }
  196. }
  197. /**********************************************************************/
  198. #pragma mark - Public Methods
  199. /**********************************************************************/
  200. - (BOOL)willShowKeyboard{
  201. if ([self firstResponder] == nil) {
  202. return NO;
  203. }
  204. return YES;
  205. }
  206. - (CGFloat)marginToBottom{
  207. return 255;
  208. }
  209. - (CGPoint)centerWithAnimation:(AlertAnimation)animation{
  210. CGRect bounds = self.window.rootViewController.view.bounds;
  211. CGPoint center = CGPointZero;
  212. switch (animation) {
  213. case kAlertAnimationTop:{
  214. center.x = bounds.size.width/2;
  215. center.y = 0-bounds.size.height/2;
  216. }break;
  217. case kAlertAnimationBottom:{
  218. center.x = bounds.size.width/2;
  219. center.y = bounds.size.height+self.bounds.size.height/2;
  220. }break;
  221. case kAlertAnimationLeft:{
  222. center.x = 0-bounds.size.width/2;
  223. if ([self willShowKeyboard]) {
  224. center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]);
  225. } else {
  226. center.y = bounds.size.height/2;
  227. }
  228. }break;
  229. case kAlertAnimationRight:{
  230. center.x = bounds.size.width+bounds.size.width/2;
  231. if ([self willShowKeyboard]) {
  232. center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]);
  233. } else {
  234. center.y = bounds.size.height/2;
  235. }
  236. }break;
  237. case kAlertAnimationFade:
  238. case kAlertAnimationPop:
  239. case kAlertAnimationNone:
  240. default:{
  241. center.x = bounds.size.width/2;
  242. if ([self willShowKeyboard]) {
  243. center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]);
  244. } else {
  245. center.y = bounds.size.height/2;
  246. }
  247. }break;
  248. }
  249. return center;
  250. }
  251. /**********************************************************************/
  252. #pragma mark - UIKeyboard
  253. /**********************************************************************/
  254. - (void)keyboardWillShow:(CGRect)frame duration:(NSTimeInterval)duration curve:(NSUInteger)curve{
  255. if (_isShow) {
  256. CGRect bounds = self.window.rootViewController.view.bounds;
  257. CGPoint center = self.center;
  258. center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]);
  259. [UIView animateWithDuration:duration delay:0 options:curve animations:^{
  260. [self setCenter:center];
  261. } completion:nil];
  262. }
  263. }
  264. - (void)keyboardWillHide:(CGRect)frame duration:(NSTimeInterval)duration curve:(NSUInteger)curve{
  265. if (_isShow) {
  266. CGRect bounds = self.window.rootViewController.view.bounds;
  267. CGPoint center = self.center;
  268. center.y = bounds.size.height/2;
  269. [UIView animateWithDuration:duration delay:0 options:curve animations:^{
  270. [self setCenter:center];
  271. } completion:nil];
  272. }
  273. }
  274. @end