123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- //
- // 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.6f]
- #import "BSAlertView.h"
- //#import "IQKeyboardManager.h"
- #import "UIView+XTExtension.h"
- @interface AlertController : UIViewController
- @property (nonatomic, assign) UIStatusBarStyle statusBarStyle;
- @end
- @implementation AlertController
- - (UIStatusBarStyle)preferredStatusBarStyle{
- return _statusBarStyle;
- }
- - (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.allowAbserveKeyboard = YES;
- [self awakeFromNib];
- }
- return self;
- }
- - (void)awakeFromNib{
-
- [super awakeFromNib];
-
- self.backgroundColor = [UIColor clearColor];
- self.autoresizingMask =
- UIViewAutoresizingFlexibleTopMargin|
- UIViewAutoresizingFlexibleBottomMargin|
- UIViewAutoresizingFlexibleLeftMargin|
- UIViewAutoresizingFlexibleRightMargin;
-
- }
- /**********************************************************************/
- #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;
-
- if (self.allowAbserveKeyboard)
- {
- //监听键盘
- // [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];
- [controller setStatusBarStyle:_statusBarStyle];
- [_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]];
- if (self.bgColor) {
- [self.alertWindow setBackgroundColor:self.bgColor];
- }else{
- [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];
- if (self.bgColor) {
- [self.alertWindow setBackgroundColor:self.bgColor];
- }else{
- [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];
- if (self.bgColor) {
- [self.alertWindow setBackgroundColor:self.bgColor];
- }else{
- [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]];
- if (self.bgColor) {
- [self.alertWindow setBackgroundColor:self.bgColor];
- }else{
- [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
|