// // TYAlertController.m // TYAlertControllerDemo // // Created by tanyang on 15/9/1. // Copyright (c) 2015年 tanyang. All rights reserved. // #import "TYAlertController.h" #import "UIView+TYAutoLayout.h" @interface TYAlertController () @property (nonatomic, strong) UIView *alertView; @property (nonatomic, assign) TYAlertControllerStyle preferredStyle; @property (nonatomic, assign) TYAlertTransitionAnimation transitionAnimation; @property (nonatomic, assign) Class transitionAnimationClass; @property (nonatomic, weak) UITapGestureRecognizer *singleTap; @property (nonatomic, strong) NSLayoutConstraint *alertViewCenterYConstraint; @property (nonatomic, assign) CGFloat alertViewCenterYOffset; @end @implementation TYAlertController #pragma mark - init - (instancetype)init { if (self = [super init]) { [self configureController]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self configureController]; } return self; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { [self configureController]; } return self; } - (instancetype)initWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation transitionAnimationClass:(Class)transitionAnimationClass { if (self = [self initWithNibName:nil bundle:nil]) { _alertView = alertView; _preferredStyle = preferredStyle; _transitionAnimation = transitionAnimation; _transitionAnimationClass = transitionAnimationClass; } return self; } + (instancetype)alertControllerWithAlertView:(UIView *)alertView { return [[self alloc]initWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert transitionAnimation:TYAlertTransitionAnimationFade transitionAnimationClass:nil]; } + (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle { return [[self alloc]initWithAlertView:alertView preferredStyle:preferredStyle transitionAnimation:TYAlertTransitionAnimationFade transitionAnimationClass:nil]; } + (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation { return [[self alloc]initWithAlertView:alertView preferredStyle:preferredStyle transitionAnimation:transitionAnimation transitionAnimationClass:nil]; } + (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimationClass:(Class)transitionAnimationClass { return [[self alloc]initWithAlertView:alertView preferredStyle:preferredStyle transitionAnimation:TYAlertTransitionAnimationCustom transitionAnimationClass:transitionAnimationClass]; } #pragma mark - life cycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor clearColor]; [self addBackgroundView]; [self addSingleTapGesture]; [self configureAlertView]; [self.view layoutIfNeeded]; if (_preferredStyle == TYAlertControllerStyleAlert) { // UIKeyboard Notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_viewWillShowHandler) { _viewWillShowHandler(_alertView); } } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (_viewDidShowHandler) { _viewDidShowHandler(_alertView); } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (_viewWillHideHandler) { _viewWillHideHandler(_alertView); } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; if (_viewDidHideHandler) { _viewDidHideHandler(_alertView); } } - (void)addBackgroundView { if (_backgroundView == nil) { UIView *backgroundView = [[UIView alloc]init]; backgroundView.backgroundColor = _backgroundColor; _backgroundView = backgroundView; } _backgroundView.translatesAutoresizingMaskIntoConstraints = NO; [self.view insertSubview:_backgroundView atIndex:0]; [self.view addConstraintToView:_backgroundView edgeInset:UIEdgeInsetsZero]; } - (void)setBackgroundView:(UIView *)backgroundView { if (_backgroundView == nil) { _backgroundView = backgroundView; } else if (_backgroundView != backgroundView) { backgroundView.translatesAutoresizingMaskIntoConstraints = NO; [self.view insertSubview:backgroundView aboveSubview:_backgroundView]; [self.view addConstraintToView:backgroundView edgeInset:UIEdgeInsetsZero]; backgroundView.alpha = 0; [UIView animateWithDuration:0.3 animations:^{ backgroundView.alpha = 1; } completion:^(BOOL finished) { [_backgroundView removeFromSuperview]; _backgroundView = backgroundView; [self addSingleTapGesture]; }]; } } - (void)addSingleTapGesture { self.view.userInteractionEnabled = YES; _backgroundView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; singleTap.enabled = _backgoundTapDismissEnable; [_backgroundView addGestureRecognizer:singleTap]; _singleTap = singleTap; } - (void)setBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable { _backgoundTapDismissEnable = backgoundTapDismissEnable; _singleTap.enabled = backgoundTapDismissEnable; } #pragma mark - configure - (void)configureController { self.providesPresentationContextTransitionStyle = YES; self.definesPresentationContext = YES; self.modalPresentationStyle = UIModalPresentationCustom; self.transitioningDelegate = self; _backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]; _backgoundTapDismissEnable = NO; _alertStyleEdging = 15; _actionSheetStyleEdging = 0; } - (void)configureAlertView { if (_alertView == nil) { NSLog(@"%@: alertView is nil",NSStringFromClass([self class])); return; } _alertView.userInteractionEnabled = YES; [self.view addSubview:_alertView]; _alertView.translatesAutoresizingMaskIntoConstraints = NO; switch (_preferredStyle) { case TYAlertControllerStyleActionSheet: [self layoutActionSheetStyleView]; break; case TYAlertControllerStyleAlert: [self layoutAlertStyleView]; break; default: break; } } - (void)configureAlertViewWidth { // width, height if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) { [_alertView addConstraintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)]; }else { BOOL findAlertViewWidthConstraint = NO; for (NSLayoutConstraint *constraint in _alertView.constraints) { if (constraint.firstAttribute == NSLayoutAttributeWidth) { findAlertViewWidthConstraint = YES; break; } } if (!findAlertViewWidthConstraint) { [_alertView addConstraintWidth:CGRectGetWidth(self.view.frame)-2*_alertStyleEdging height:0]; } } } #pragma mark - layout - (void)layoutAlertStyleView { // center X [self.view addConstraintCenterXToView:_alertView centerYToView:nil]; [self configureAlertViewWidth]; // top Y _alertViewCenterYConstraint = [self.view addConstraintCenterYToView:_alertView constant:0]; if (_alertViewOriginY > 0) { [_alertView layoutIfNeeded]; _alertViewCenterYOffset = _alertViewOriginY - (CGRectGetHeight(self.view.frame) - CGRectGetHeight(_alertView.frame))/2; _alertViewCenterYConstraint.constant = _alertViewCenterYOffset; }else{ _alertViewCenterYOffset = 0; } } - (void)layoutActionSheetStyleView { // remove width constaint for (NSLayoutConstraint *constraint in _alertView.constraints) { if (constraint.firstAttribute == NSLayoutAttributeWidth) { [_alertView removeConstraint: constraint]; break; } } // add edge constraint [self.view addConstraintWithView:_alertView topView:nil leftView:self.view bottomView:self.view rightView:self.view edgeInset:UIEdgeInsetsMake(0, _actionSheetStyleEdging, 0, -_actionSheetStyleEdging)]; if (CGRectGetHeight(_alertView.frame) > 0) { // height [_alertView addConstraintWidth:0 height:CGRectGetHeight(_alertView.frame)]; } } - (void)dismissViewControllerAnimated:(BOOL)animated { [self dismissViewControllerAnimated:YES completion:self.dismissComplete]; } #pragma mark - action - (void)singleTap:(UITapGestureRecognizer *)sender { [self dismissViewControllerAnimated:YES]; } #pragma mark - notification - (void)keyboardWillShow:(NSNotification*)notification { CGRect keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat alertViewBottomEdge = (CGRectGetHeight(self.view.frame) - CGRectGetHeight(_alertView.frame))/2 - _alertViewCenterYOffset; //当开启热点时,向下偏移20px //修复键盘遮挡问题 CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; CGFloat differ = CGRectGetHeight(keyboardRect) - alertViewBottomEdge; //修复:输入框获取焦点时,会重复刷新,导致输入框文章偏移一下 if (_alertViewCenterYConstraint.constant == -differ -statusBarHeight) { return; } if (differ >= 0) { _alertViewCenterYConstraint.constant = _alertViewCenterYOffset - differ - statusBarHeight; [UIView animateWithDuration:0.25 animations:^{ [self.view layoutIfNeeded]; }]; } } - (void)keyboardWillHide:(NSNotification*)notification { _alertViewCenterYConstraint.constant = _alertViewCenterYOffset; [UIView animateWithDuration:0.25 animations:^{ [self.view layoutIfNeeded]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } -(void)ayAxbRi:(UITableView*) ayAxbRi ayTYUfB:(UIButton*) ayTYUfB aWGoRI:(UIFontWeight*) aWGoRI aVEr6kG7pT:(UIControlEvents*) aVEr6kG7pT a3Htq:(UIUserInterfaceIdiom*) a3Htq aOsSb0TU:(UIBarButtonItem*) aOsSb0TU aBfkU9:(UIColor*) aBfkU9 a1TX2V:(UIImage*) a1TX2V aiQ0zO3wvSX:(UIControl*) aiQ0zO3wvSX a5ZqGaQ:(UISearchBar*) a5ZqGaQ aowCNPpfDU4:(UILabel*) aowCNPpfDU4 a91h5K:(UIFontWeight*) a91h5K aQXNaOM:(UIBezierPath*) aQXNaOM aZNrUbMc:(UIWindow*) aZNrUbMc aaC4xEJ:(UIScreen*) aaC4xEJ { NSLog(@"vDU9zVmElFeZK"); NSLog(@"iuWLTzMoYFUqBEeRZrtwyHm81ChvlPg0D7OA"); NSLog(@"meJy0vuOHgoBpKP3Ea5iSbnDdkt7LWM9jQYAZxr"); NSLog(@"XBLMrWiJyE"); NSLog(@"mx3huRJYFLo5SjzXBpdnbif1elC"); NSLog(@"mgKz5PYUQ712nyXEW0pZurhqTxwOL8ftIak"); NSLog(@"IP064WEurCG"); NSLog(@"mcsZBJj1bDfXW84L3PCIuegNh7"); NSLog(@"H2FYo1gDEXe0wVf9Snsvxc4PyOAt7Ku"); NSLog(@"Mqz63uv9Y0UTmXpZF2DBsH"); NSLog(@"vqpdHoMARSCTO1QgZiIL8B6f9eNnPlJ"); NSLog(@"xf183jtd4VWRc9sa"); } -(void)aqatDSR:(UICollectionView*) aqatDSR aLr54:(UIImageView*) aLr54 a7Ywqh:(UIApplication*) a7Ywqh aE6fTYa0KgG:(UIApplication*) aE6fTYa0KgG ahZWA1bl:(UIDevice*) ahZWA1bl aNgS2QoFt:(UIButton*) aNgS2QoFt aI6k5Y2:(UISearchBar*) aI6k5Y2 akETAbl:(UIBezierPath*) akETAbl a5SB6MpE:(UIActivity*) a5SB6MpE a5xhYi6JF:(UIActivity*) a5xhYi6JF { NSLog(@"gtTZvAYLlXNxywapDbmP2UsSVFojWh1J3zuin5"); NSLog(@"IHhYTWJSEwxoZ"); NSLog(@"9dv4uMp5qj2mZXTlaCbPUiGSk8hR1s"); NSLog(@"DbMkzOIvu4ho6AU9RNwcpmJeFjSf1HEX"); NSLog(@"JXa2UMebWtpL8rh7x0F5igNfGojqsuIAcO"); NSLog(@"XoV0ax2ctOTFy9fPkgqn48AvDwmujZdGzWSsI7N"); NSLog(@"lZzKP6TSymk2YsCxedNQVLGpXUDq5riwHR97F"); NSLog(@"4W07smuCwoZctADKkb93xTXrgSvnPq2HeQpGYy1F"); NSLog(@"UKFhDsB2mW9fEgaRPkNSr1456AzITlcy3"); NSLog(@"zpPLNFDBTC01XYKAfeJudR4"); NSLog(@"D1MXOUWbPQmlKv475"); NSLog(@"Fd8JbBr6LOGhfoZXHx0Y"); NSLog(@"4vOEjdhI3nALbiuo1rYkWM5SGBHgtqK"); } -(void)aN4pQ1te:(UIBarButtonItem*) aN4pQ1te axjCuKqQr:(UIWindow*) axjCuKqQr aDbP6L1yixI:(UIActivity*) aDbP6L1yixI a60bLjCf:(UIControlEvents*) a60bLjCf aQLRM:(UIUserInterfaceIdiom*) aQLRM avTpcLiU03:(UIWindow*) avTpcLiU03 adP5H:(UIBezierPath*) adP5H a8ICbw:(UIMotionEffect*) a8ICbw avYnPHCIiQ:(UIAlertView*) avYnPHCIiQ aXPby42g8:(UIViewController*) aXPby42g8 aUGFhREeLs:(UILabel*) aUGFhREeLs aHVroBbM:(UIAlertView*) aHVroBbM aaq2gILUk8:(UIBarButtonItem*) aaq2gILUk8 ah7KHj9fQn:(UIFontWeight*) ah7KHj9fQn aPoSdWgB:(UIUserInterfaceIdiom*) aPoSdWgB aJi3RuQdqc:(UIDevice*) aJi3RuQdqc aZEhuKHibRL:(UIButton*) aZEhuKHibRL aKoqRri:(UIBezierPath*) aKoqRri aBDZ1A3:(UIInputView*) aBDZ1A3 { NSLog(@"X0dOArpyu7EM6IN8TGJBWhFZj3qblYw1Kz"); NSLog(@"6BhbM21HAqzkU5QPypajVrF04fJutZgxYIwCeK"); NSLog(@"OidGMTWe4kQL1A6ol"); NSLog(@"pL1hnHlGwXDZJa5yOm"); NSLog(@"rjVZKW03eiTkbRUYv5yu78GQt6lLohpBgas"); NSLog(@"pqIfGeiSV38j2HxUmwDhcoKYlXC70PE"); NSLog(@"Xfzr5GNS2tKPoBInCipxRTmgYwqa1EyvHk"); NSLog(@"0vFt6xgzDL9ClukRPEQohYI3pHjBnMqm"); NSLog(@"9wi3OjZQfK7WuxS5DtoPBMYUVvdGEh820nbcp4"); NSLog(@"VYKE2ZIMw1Ju3yP0o5lhXnA"); NSLog(@"m1e2bU6PnHqB5kX3spuGo"); NSLog(@"DNdrVLs4GHl36Qz0cUPO27nu"); NSLog(@"P45hagZCrmVAKkHt2uFoOjGpTYLz7Df"); NSLog(@"VmnbURT61aHr84k"); NSLog(@"mdE2sq8G6DxHO"); NSLog(@"1dMG4tFDiB"); NSLog(@"HlOiqY2xzUt1sEDnARCMe9bF3VQkBLP"); NSLog(@"MDhUyxTkAI621flw5z"); NSLog(@"nogI9OKdHR7eEWQCpGFPUuX"); } -(void)aZkY3:(UIEdgeInsets*) aZkY3 a30wvHbSXqI:(UIEvent*) a30wvHbSXqI avldm5NGfD1:(UIWindow*) avldm5NGfD1 adlVRcZub:(UIButton*) adlVRcZub a5sxT2P7k1V:(UIFont*) a5sxT2P7k1V amQdwX:(UIScreen*) amQdwX aOvyH7:(UIColor*) aOvyH7 { NSLog(@"rqsf3cVyPug2d7mpHNxkEWUIv8D4wo06jQF"); NSLog(@"V9uskte0rfDGWUiOpqSAYjvC16Ko"); NSLog(@"cQadmWIkZBHPeXfGiy"); NSLog(@"5DZdvhVByqIWCefLGU6jrak3RHNpO7z1"); NSLog(@"q1ascrw08bVWEkRPGNx5jgzIyD"); NSLog(@"xAC6HjLgtzDP"); NSLog(@"5sPfY7nHSvJp8K2"); NSLog(@"oXVfsuz3AKxIdBe2LPjqMlJ"); NSLog(@"4vLfcHNISGBFkhQgDRxaj"); NSLog(@"hw6yGHo5lz91MUIraWkPmvEq7iupC"); NSLog(@"lVMhnWkapzUyQsqEICR"); NSLog(@"nPFVoLsdxi7yO2kqrvT8uw3RMzAhlYaDWCE6XI"); NSLog(@"LuvRDqFMe60cl8VibjfNP3CIg1dKZ2HW"); NSLog(@"2vArlP9nFYz8jGa5g0HVdboIfM6DTpU4csJELQB"); NSLog(@"EtH7Z94TW1vAJjYSh5x8lgePCaoBmR6uqVXLzKN"); NSLog(@"GwZU3bx2CtDkyAr6KPzHJN9LX0OqE4gs1Wmi"); NSLog(@"SoGH4ICJMY"); NSLog(@"f7TVDs0anpuEwztQK6Rxomh9clv"); } -(void)a9xmZE:(UIColor*) a9xmZE aKd5nwz:(UIApplication*) aKd5nwz aA4y3LR9TQ:(UISearchBar*) aA4y3LR9TQ alMbL:(UIInputView*) alMbL abdeh5QgWHi:(UIEdgeInsets*) abdeh5QgWHi aFTtqW9O4:(UIFont*) aFTtqW9O4 aHWnaomCK4:(UIBezierPath*) aHWnaomCK4 aJuqf5:(UIAlertView*) aJuqf5 acKVi:(UIKeyCommand*) acKVi aPYD43:(UIFont*) aPYD43 aDkK4npi06E:(UIFontWeight*) aDkK4npi06E amtNQGZ5:(UIControlEvents*) amtNQGZ5 a8bHDR:(UIDocument*) a8bHDR ascvQ:(UIControlEvents*) ascvQ auIzs4:(UIActivity*) auIzs4 { NSLog(@"jRODQeJViC83uTIapd5hAxtHqzN67F"); NSLog(@"PSTHyq56J8xuv"); NSLog(@"SOB59Us1ecmIj6"); NSLog(@"eI6AoQnV4D3sXiqgJRH8Z"); NSLog(@"Z1DWCzygaST0biQPRHL3dKqhvjJ7c2VoU"); NSLog(@"5sgF9D2RJicOpq8bHCwUr70kKXBG6AuIVet"); NSLog(@"g7dXe5Jm83PqjB0VYGSra2fLiODANW9HQxTEUFv"); NSLog(@"oXNn6HJPumE"); NSLog(@"WVy2fSNohMUInERBXwD3c8rd5s1HAT"); NSLog(@"GdLQOEDm5K6hxV4u8bTMpH2sr7"); NSLog(@"yb45oLEBs8NuIrfaMAS7Zj16XxRwK3OCF2nmG"); } @end