自定义弹出框

xiaoxiao2021-02-28  84

// //  WJViewController.m //  WJAlertView // //  Created by 华通众和 on 16/5/5. //  Copyright © 2016年 华鑫志和科技. All rights reserved. // #import "WJViewController.h" #import "WJAlertViewController.h" @interface WJViewController () @end @implementation WJViewController - (void)viewDidLoad {     [super viewDidLoad];     UIButton * alertButton = [UIButton buttonWithType:UIButtonTypeCustom];     alertButton.frame = CGRectMake(0, 300, self.view.frame.size.width, 50);     [alertButton setBackgroundColor:[UIColor grayColor]];     [alertButton addTarget:self action:@selector(alertAction:) forControlEvents:UIControlEventTouchUpInside];     [self.view addSubview:alertButton]; } - (void)alertAction:(UIButton *)btn {     WJAlertViewController *WJVC = [[WJAlertViewController alloc] initWithTransitionStyle:TransitionDefault determineAction:^(NSString *determineText) {         NSLog(@"determineText == %@",determineText);     } andCancelAction:^{         NSLog(@"点击了取消按钮");     }];          [self presentViewController:WJVC animated:YES completion:nil]; }

// //  WJAlertViewController.h //  WJAlertView // //  Created by 华通众和 on 16/5/5. //  Copyright © 2017年 华鑫志和科技. All rights reserved. // #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, TransitionStyle) {     TransitionVertical = 0,  //底部向上     TransitionHorizontal ,   //翻转     TransitionDefault,       //中间弹出 }; @interface WJAlertViewController : UIViewController typedef void(^DetermineBlock)(NSString * determineText);        //返回输入文字 typedef void(^CancelBlock)();                                   //返回取消事件 @property (nonatomic, assign) TransitionStyle transitionStyle;  //弹出样式 - (instancetype)initWithTransitionStyle:(TransitionStyle)transitionStyle determineAction:(DetermineBlock)determineBlock andCancelAction:(CancelBlock)cancelBlcok; @end

// //  WJAlertViewController.m //  WJAlertView // //  Created by 华通众和 on 16/5/5. //  Copyright © 2017年 华鑫志和科技. All rights reserved. // #import "WJAlertViewController.h" #define WJScreenWidth  [[UIScreen mainScreen] bounds].size.width #define WJScreenHeight [[UIScreen mainScreen] bounds].size.height #define OnePixel     (1./[UIScreen mainScreen].scale) #define animateTime  0.35f #define RGB(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a] #define WS(weakSelf) __unsafe_unretained __typeof(&*self)weakSelf = self; @interface WJAlertViewController () @property (nonatomic, assign)   BOOL notifiKeyboardHide; @property (nonatomic, strong)   UIView * operateView;           //操作视图 @property (nonatomic, strong)   UIButton * cancelBtn;           //取消按钮 @property (nonatomic, strong)   UIButton * confirmBtn;          //确定按钮 @property (nonatomic, strong)   UILabel * horLine;              //分割线 @property (nonatomic, strong)   UILabel * verLine;              //分割线 @property (nonatomic, strong)   UILabel *titleLabel;            //系统提示 @property (nonatomic, strong)   UIView * inputBkView;           //输入框背景 @property (nonatomic, strong)   UITextView *noteTV;             //输入框 @property (nonatomic, strong)   UILabel *placeHolderLabel;      //默认提示文字 @property (nonatomic, copy)     DetermineBlock determineBlock;  //确定按钮 @property (nonatomic, copy)     CancelBlock cancelBlock;        //返回按钮 @end @implementation WJAlertViewController - (instancetype)initWithTransitionStyle:(TransitionStyle)transitionStyle determineAction:(DetermineBlock)determineBlock andCancelAction:(CancelBlock)cancelBlcok {          if (self = [super init]) {         self.determineBlock = determineBlock;         self.cancelBlock = cancelBlcok;         self.modalPresentationStyle = UIModalPresentationFormSheet;         switch (transitionStyle) {             case (0):                 self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;                 break;             case (1):                 self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;                 break;             default:                 self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;                 break;         }              }     return self; } - (void)viewDidLoad {     [super viewDidLoad];     self.view.backgroundColor = RGB(127, 127, 127, 1); } - (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];     [self showAlertView]; } #pragma mark - ----------------创建UI ------------------------- - (void)showAlertView {     _notifiKeyboardHide = NO;          [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(keyboardWillShow:)                                                  name:UIKeyboardWillShowNotification                                                object:nil];               [self shakeToShow:self.operateView];                //弹出框     [self.operateView addSubview:self.horLine];         //分割线     [self.operateView addSubview:self.verLine];         //分割线     [self.operateView addSubview:self.titleLabel];      //系统提示     [self.operateView addSubview:self.inputBkView];     //输入框背景     [self.operateView addSubview:self.cancelBtn];       //返回按钮     [self.operateView addSubview:self.confirmBtn];      //确定按钮     [self.inputBkView addSubview:self.noteTV];          //输入框     [self.noteTV addSubview:self.placeHolderLabel];     //默认文字     [self.view addSubview:self.operateView];      } #pragma mark -------------- 移除视图 ----------------- - (void)removeAlertView {     if ([_noteTV isFirstResponder]) {         [_noteTV resignFirstResponder];     }          [UIView animateWithDuration:0.15 animations:^{         _operateView.alpha = 0;         _operateView.transform = CGAffineTransformMakeScale(0.1, 0.1);     } completion:^(BOOL finished) {         [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];         if (_notifiKeyboardHide) {             [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];         }                  [self dismissViewControllerAnimated:YES completion:nil];     }]; } - (void)confirmAction:(UIButton *)sender {     WS(weakSelf);     if (weakSelf.determineBlock) {         weakSelf.determineBlock(_noteTV.text);     }          [weakSelf removeAlertView]; } - (void)cancelAction:(UIButton *)sender {     WS(weakSelf);     if (weakSelf.cancelBlock) {         weakSelf.cancelBlock();     }     [weakSelf removeAlertView]; } - (void)textChanged:(NSNotification *)notification {     if ([[self.noteTV text] length] == 0) {         [[self.noteTV viewWithTag:999] setAlpha:1];     }     else {         [[self.noteTV viewWithTag:999] setAlpha:0];     } } #pragma mark -------------- 监听键盘弹起,操作框动画 ----------------- ///键盘弹起,页面动画,监听 - (void)keyboardWillShow:(NSNotification *)notification {     //键盘的frame     CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];     //键盘高度     CGFloat keyboardHeight = keyboardRect.size.height;     //键盘升起后 键盘的Y坐标     CGFloat keyboardOriginY = WJScreenHeight - keyboardHeight;     //键盘升起后 提示框的Y坐标     CGFloat operateMaxY = WJScreenHeight/2. + _operateView.bounds.size.height/2. + 16;          if (operateMaxY >= keyboardOriginY) {         [UIView animateWithDuration:0.25 animations:^{             CGRect rect = _operateView.frame;             rect.origin.y = keyboardOriginY - rect.size.height - 16;             _operateView.frame = rect;                      } completion:^(BOOL finished) {                      }];         _notifiKeyboardHide = YES;         [[NSNotificationCenter defaultCenter] addObserver:self                                                  selector:@selector(keyboardWillHide:)                                                      name:UIKeyboardWillHideNotification                                                    object:nil];     }     else {         _notifiKeyboardHide = NO;     } } #pragma mark -------------- 键盘收起,页面动画,监听 ----------------- - (void)keyboardWillHide:(NSNotification *)notification {     [UIView animateWithDuration:0.25 animations:^{         CGRect rect = _operateView.frame;         rect.origin.y = (WJScreenHeight - rect.size.height)/2.;         _operateView.frame = rect;     } completion:^(BOOL finished) {              }]; } #pragma mark -------------- 颜色转换为图片 ----------------- - (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)aSize {     CGRect rect = CGRectMake(0.0f, 0.0f, aSize.width, aSize.height);     UIGraphicsBeginImageContext(rect.size);     CGContextRef context = UIGraphicsGetCurrentContext();          CGContextSetFillColorWithColor(context, [color CGColor]);     CGContextFillRect(context, rect);          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();          return image; } #pragma mark ---------------- 弹性震颤动画 ------------------- - (void)shakeToShow:(UIView *)aView {     CAKeyframeAnimation * popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];     popAnimation.duration = 0.35;     popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],                             [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05f, 1.05f, 1.0f)],                             [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],                             [NSValue valueWithCATransform3D:CATransform3DIdentity]];     popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @0.8f];     popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];     [aView.layer addAnimation:popAnimation forKey:nil]; } #pragma mark ---------------- 懒加载 ------------------- - (UIView *)operateView {     if (_operateView == nil) {         _operateView = [[UIView alloc] init];         _operateView.center = CGPointMake(WJScreenWidth/2., WJScreenHeight/2.);         _operateView.bounds = CGRectMake(0, 0, WJScreenWidth - 32, 208);         _operateView.backgroundColor = [UIColor whiteColor];         _operateView.layer.cornerRadius = 6;         _operateView.clipsToBounds = YES;     }     return _operateView; } - (UIButton *)cancelBtn {     if (_cancelBtn == nil) {         _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];         _cancelBtn.frame = CGRectMake(0, CGRectGetHeight(_operateView.frame) - 48, _operateView.frame.size.width/2., 48);         [_cancelBtn setTitleColor:RGB(51, 51, 51, 1) forState:UIControlStateNormal];         [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];         _cancelBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];         [_cancelBtn setBackgroundImage:[self imageWithColor:RGB(255, 255, 255, 1) andSize:_cancelBtn.bounds.size] forState:UIControlStateNormal];         [_cancelBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];     }     return _cancelBtn; } - (UIButton *)confirmBtn {     if (_confirmBtn == nil) {                  _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];         _confirmBtn.frame = CGRectMake(_operateView.frame.size.width/2., CGRectGetHeight(_operateView.frame) - 48, _operateView.frame.size.width/2., 48) ;         [_confirmBtn setTitleColor:RGB(51, 51, 51, 1) forState:UIControlStateNormal];         [_confirmBtn setTitle:@"确认" forState:UIControlStateNormal];         _confirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];         [_confirmBtn setBackgroundImage:[self imageWithColor:RGB(255, 255, 255, 1) andSize:_cancelBtn.bounds.size] forState:UIControlStateNormal];         [_confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];     }     return _confirmBtn; } - (UILabel *)horLine {     if (_horLine == nil) {         _horLine = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_operateView.frame) - 48 - OnePixel, _operateView.frame.size.width, OnePixel)];         _horLine.backgroundColor = RGB(230, 230, 230, 1);     }     return _horLine; } - (UILabel *)verLine {     if (_verLine == nil) {         _verLine = [[UILabel alloc] initWithFrame:CGRectMake(_operateView.frame.size.width/2. - OnePixel/2., CGRectGetHeight(_operateView.frame) - 48 - OnePixel, OnePixel, 48)];         _verLine.backgroundColor = RGB(128 , 128, 128, 1);     }     return _verLine; } - (UILabel *)titleLabel {     if (_titleLabel == nil) {         _titleLabel = [[UILabel alloc] init];         _titleLabel.bounds = CGRectMake(0, 0, CGRectGetWidth(_operateView.frame), 20);         _titleLabel.center = CGPointMake(CGRectGetMidX(_operateView.bounds), 20);         _titleLabel.text = @"拒绝原因";         _titleLabel.textAlignment = NSTextAlignmentCenter;     }     return _titleLabel; } - (UIView *)inputBkView {     if (_inputBkView == nil) {         _inputBkView = [[UIView alloc] init];         _inputBkView.layer.borderColor = RGB(230, 230, 230, 1).CGColor;         _inputBkView.layer.borderWidth = 1;         _inputBkView.bounds = CGRectMake(0, 0, CGRectGetWidth(_operateView.frame) - 64, 70);         _inputBkView.center = CGPointMake(CGRectGetMidX(_operateView.bounds), 32 + 24 + 40);     }     return _inputBkView; } - (UITextView *)noteTV {     if (_noteTV == nil) {         _noteTV = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_inputBkView.bounds), CGRectGetHeight(_inputBkView.bounds))];         _noteTV.textColor = [UIColor blackColor];         _noteTV.backgroundColor = [UIColor whiteColor];         _noteTV.font = [UIFont systemFontOfSize:17.0];         _noteTV.layer.borderColor = [UIColor colorWithRed:221 / 256.0 green:221 / 256.0 blue:221 / 256.0 alpha:1].CGColor;         _noteTV.layer.borderWidth = 1;         _noteTV.layer.cornerRadius = 3;         _noteTV.layer.masksToBounds = YES;                  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];     }     return _noteTV; } - (UILabel *)placeHolderLabel{     if (_placeHolderLabel == nil) {         _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 4, 200, 30)];         _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;         _placeHolderLabel.font = [UIFont systemFontOfSize:17.0];         _placeHolderLabel.textColor = [UIColor colorWithRed:221 / 256.0 green:221 / 256.0 blue:221 / 256.0 alpha:1];         _placeHolderLabel.backgroundColor = [UIColor clearColor];         _placeHolderLabel.tag = 999;         _placeHolderLabel.text = @"请输入内容";         if ([self.noteTV.text length] == 0) {             [[self.noteTV viewWithTag:999] setAlpha:1];         }     }     return _placeHolderLabel; } - (void)dealloc {     [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];      }

转载请注明原文地址: https://www.6miu.com/read-30446.html

最新回复(0)