// // BSAlertView.m // Unionpay // // Created by Qing Xiubin on 13-8-7. // Copyright (c) 2013年 成都中信联通科技有限公司. All rights reserved. // #if !__has_feature(objc_arc) #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). #endif #define ALERT_TIME_DURATION 0.3f #define ALERT_ALPHA_HIDE 0.0f #define ALERT_ALPHA_SHOW 1.0f #define ALERT_COLOR_HIDE [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f] #define ALERT_COLOR_SHOW [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f] #import "BSAlertView.h" #import "IQKeyboardManager.h" @interface AlertController : UIViewController @end @implementation AlertController - (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleDefault; } - (BOOL)prefersStatusBarHidden{ return NO; } - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation{ return UIStatusBarAnimationFade; } -(BOOL)shouldAutorotate { return YES; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } @end @interface BSAlertView () @property (assign, nonatomic) BOOL isShow; @property (strong, nonatomic) UIWindow *alertWindow; @property (strong, nonatomic) UIWindow *previousWindow; @end @implementation BSAlertView - (instancetype)init{ return [super initWithFrame:CGRectZero]; } - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self awakeFromNib]; } return self; } - (void)awakeFromNib{ [super awakeFromNib]; self.backgroundColor = [UIColor clearColor]; self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin| UIViewAutoresizingFlexibleBottomMargin| UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin; //设置背景 if (_bgImageView == nil) { _bgImageView = [[UIImageView alloc] initWithFrame:self.bounds]; _bgImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleTopMargin| UIViewAutoresizingFlexibleBottomMargin| UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin; [self insertSubview:_bgImageView atIndex:0]; } _bgImageView.image = [UIImage stretchableImageNamed:@"common_middle_bg"]; if (_gesture) { } } /**********************************************************************/ #pragma mark - Public Methods /**********************************************************************/ - (void)show{ return [self showWithAnimation:kAlertAnimationBottom]; } - (void)bgViewTapAction:(UITapGestureRecognizer *)sender{ [self dismiss]; } - (void)showWithAnimation:(AlertAnimation)animation{ if (_isShow == YES) { return; } _isShow = YES; //监听键盘 [self observeKeyboard]; //设置Window [self setPreviousWindow:[[UIApplication sharedApplication] keyWindow]]; _alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [_alertWindow setWindowLevel:UIWindowLevelAlert]; [_alertWindow setBackgroundColor:ALERT_COLOR_HIDE]; AlertController *controller = [[AlertController alloc] init]; [_alertWindow setRootViewController:controller]; [_alertWindow.rootViewController.view addSubview:self]; [_alertWindow makeKeyAndVisible]; if (_gesture) { UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom]; [bgButton setFrame:controller.view.bounds]; [bgButton setBackgroundColor:[UIColor clearColor]]; [bgButton addTarget:self action:@selector(bgViewTapAction:) forControlEvents:UIControlEventTouchUpInside]; [controller.view insertSubview:bgButton belowSubview:self]; } //弹窗动画 switch (animation) { case kAlertAnimationTop: case kAlertAnimationBottom: case kAlertAnimationLeft: case kAlertAnimationRight:{ [self setAlpha:ALERT_ALPHA_SHOW]; [self setCenter:[self centerWithAnimation:animation]]; [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setCenter:[self centerWithAnimation:kAlertAnimationNone]]; [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW]; } completion:nil]; }break; case kAlertAnimationFade:{ [self setAlpha:ALERT_ALPHA_HIDE]; [self setCenter:[self centerWithAnimation:animation]]; [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setAlpha:ALERT_ALPHA_SHOW]; [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW]; } completion:nil]; }break; case kAlertAnimationPop:{ [self setAlpha:ALERT_ALPHA_HIDE]; [self setCenter:[self centerWithAnimation:animation]]; [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setAlpha:ALERT_ALPHA_SHOW]; [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW]; } completion:nil]; CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; scaleAnimation.removedOnCompletion = YES; scaleAnimation.duration = ALERT_TIME_DURATION; scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; scaleAnimation.values = @[@0.01f,@1.1f,@0.9f,@1.0f]; [self.layer addAnimation:scaleAnimation forKey:nil]; }break; case kAlertAnimationNone: default:{ [self setAlpha:ALERT_ALPHA_SHOW]; [self setCenter:[self centerWithAnimation:kAlertAnimationNone]]; [self.alertWindow setBackgroundColor:ALERT_COLOR_SHOW]; }break; } } - (void)dismiss{ return [self dismissWithAnimation:kAlertAnimationFade]; } - (void)dismissWithAnimation:(AlertAnimation)animation{ if (_isShow == NO) { return; } _isShow = NO; //隐藏键盘 [self unObserveKeyboard]; [self endEditing:YES]; //恢复Window [self.previousWindow makeKeyWindow]; //弹窗动画 switch (animation) { case kAlertAnimationTop: case kAlertAnimationBottom: case kAlertAnimationLeft: case kAlertAnimationRight:{ [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setCenter:[self centerWithAnimation:animation]]; [self.alertWindow setBackgroundColor:ALERT_COLOR_HIDE]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; }break; case kAlertAnimationFade: case kAlertAnimationPop:{ [UIView animateWithDuration:ALERT_TIME_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setAlpha:ALERT_ALPHA_HIDE]; [self.alertWindow setBackgroundColor:ALERT_COLOR_HIDE]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; }break; case kAlertAnimationNone: default:{ [self setAlpha:ALERT_ALPHA_HIDE]; [self.alertWindow setBackgroundColor:ALERT_COLOR_HIDE]; [self removeFromSuperview]; }break; } } /**********************************************************************/ #pragma mark - Public Methods /**********************************************************************/ - (BOOL)willShowKeyboard{ if ([self firstResponder] == nil) { return NO; } return YES; } - (CGFloat)marginToBottom{ return 255; } - (CGPoint)centerWithAnimation:(AlertAnimation)animation{ CGRect bounds = self.window.rootViewController.view.bounds; CGPoint center = CGPointZero; switch (animation) { case kAlertAnimationTop:{ center.x = bounds.size.width/2; center.y = 0-bounds.size.height/2; }break; case kAlertAnimationBottom:{ center.x = bounds.size.width/2; center.y = bounds.size.height+self.bounds.size.height/2; }break; case kAlertAnimationLeft:{ center.x = 0-bounds.size.width/2; if ([self willShowKeyboard]) { center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]); } else { center.y = bounds.size.height/2; } }break; case kAlertAnimationRight:{ center.x = bounds.size.width+bounds.size.width/2; if ([self willShowKeyboard]) { center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]); } else { center.y = bounds.size.height/2; } }break; case kAlertAnimationFade: case kAlertAnimationPop: case kAlertAnimationNone: default:{ center.x = bounds.size.width/2; if ([self willShowKeyboard]) { center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]); } else { center.y = bounds.size.height/2; } }break; } return center; } /**********************************************************************/ #pragma mark - UIKeyboard /**********************************************************************/ - (void)keyboardWillShow:(CGRect)frame duration:(NSTimeInterval)duration curve:(NSUInteger)curve{ if (_isShow) { CGRect bounds = self.window.rootViewController.view.bounds; CGPoint center = self.center; center.y = bounds.size.height-(self.bounds.size.height/2+[self marginToBottom]); [UIView animateWithDuration:duration delay:0 options:curve animations:^{ [self setCenter:center]; } completion:nil]; } } - (void)keyboardWillHide:(CGRect)frame duration:(NSTimeInterval)duration curve:(NSUInteger)curve{ if (_isShow) { CGRect bounds = self.window.rootViewController.view.bounds; CGPoint center = self.center; center.y = bounds.size.height/2; [UIView animateWithDuration:duration delay:0 options:curve animations:^{ [self setCenter:center]; } completion:nil]; } } @end