| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- //
- // CSAlertView.m
- // zhuxun
- //
- // Created by JonathanH on 2017/7/20.
- //
- //
- #import "CSAlertView.h"
- ///alertView 宽
- #define AlertW 280
- ///各个栏目之间的距离
- #define XLSpace 10.0
- @interface CSAlertView()
- //弹窗
- @property (nonatomic,weak) UIView *alertView;
- //title
- @property (nonatomic,weak) UILabel *titleLbl;
- //内容
- @property (nonatomic,weak) UILabel *msgLbl;
- //输入文本框
- @property (nonatomic,weak) UITextField *textField;
- //文本框内菊花
- @property (nonatomic,weak) UIActivityIndicatorView *actityindicatorview;
- //确认按钮
- @property (nonatomic,weak) UIButton *sureBtn;
- //取消按钮
- @property (nonatomic,weak) UIButton *cancleBtn;
- //横线线
- @property (nonatomic,weak) UIView *lineView;
- //竖线
- @property (nonatomic,weak) UIView *verLineView;
- @end
- @implementation CSAlertView
- - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message inputMode:(BOOL)inputmode sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
- {
- if (self == [super init]) {
- self.frame = [UIScreen mainScreen].bounds;
-
- self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
-
- UIView *alertView = [[UIView alloc] init];
- alertView.backgroundColor = [UIColor whiteColor];
- alertView.layer.cornerRadius = 5.0;
-
- alertView.frame = CGRectMake(0, 0, AlertW, 100);
- alertView.layer.position = self.center;
- [self addSubview:alertView];
- self.alertView = alertView;
- CGFloat nextX =0;
- CGFloat nextY =0;
- if (title) {
-
- UILabel *titleLbl = [self GetAdaptiveLable:CGRectMake(2*XLSpace, 2*XLSpace, AlertW-4*XLSpace, 20) AndText:title andIsTitle:YES];
- titleLbl.textAlignment = NSTextAlignmentCenter;
-
- [self.alertView addSubview:titleLbl];
- self.titleLbl = titleLbl;
- CGFloat titleW = self.titleLbl.bounds.size.width;
- CGFloat titleH = self.titleLbl.bounds.size.height;
-
- self.titleLbl.frame = CGRectMake((AlertW-titleW)/2, 2*XLSpace, titleW, titleH);
- //有标题下一个控件是 subtitle 最多2行Nextpoint 更改为(XLSpace,CG)
- nextX = XLSpace;
- nextY = CGRectGetMaxY(_titleLbl.frame)+XLSpace;
- }
- if (message) {
-
- UILabel *msgLbl = [self GetAdaptiveLable:CGRectMake(nextX, nextY, AlertW-2*XLSpace, 20) AndText:message andIsTitle:NO];
- msgLbl.textAlignment = NSTextAlignmentCenter;
- msgLbl.textColor = [UIColor redColor];
- CGFloat msgW = msgLbl.bounds.size.width;
- CGFloat msgH = msgLbl.bounds.size.height;
- msgLbl.frame = _titleLbl?CGRectMake((AlertW-msgW)/2, CGRectGetMaxY(_titleLbl.frame)+XLSpace, msgW, msgH):CGRectMake((AlertW-msgW)/2, 2*XLSpace, msgW, msgH);
- [self.alertView addSubview:msgLbl];
- self.msgLbl = msgLbl;
-
- nextY = CGRectGetMaxY(msgLbl.frame)+XLSpace;
- }
- if(inputmode){
- UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(nextX, nextY, AlertW -2*XLSpace, 22)];
- textField.borderStyle = UITextBorderStyleRoundedRect;
- textField.backgroundColor = [UIColor lightGrayColor];
- textField.clearButtonMode = UITextFieldViewModeWhileEditing;
- UIActivityIndicatorView *actityindicatorview = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- textField.rightView = actityindicatorview;
- self.actityindicatorview = actityindicatorview;
- textField.rightViewMode = UITextFieldViewModeUnlessEditing;
- textField.text = @"新建文件夹";
- [self.alertView addSubview:textField];
- self.textField = textField;
-
- nextY = CGRectGetMaxY(_textField.frame)+2*XLSpace;
-
- }
- UIView *lineView = [[UIView alloc] init];
- lineView.frame = CGRectMake(0, nextY, AlertW, 1);
- lineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
- [alertView addSubview:lineView];
- self.lineView = lineView;
-
- //两个按钮
- if (cancleTitle && sureTitle) {
- UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
- cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(_lineView.frame), (AlertW-1)/2, 40);
- [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
- [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
- [cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
- cancleBtn.tag = 1;
- [cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- // UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5.0, 5.0)];
- // CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
- // maskLayer.frame = cancleBtn.bounds;
- // maskLayer.path = maskPath.CGPath;
- // cancleBtn.layer.mask = maskLayer;
- //cancleBtn.layer.masksToBounds = YES;
- [alertView addSubview:cancleBtn];
- self.cancleBtn = cancleBtn;
-
-
-
-
- UIView *verLineView = [[UIView alloc] init];
- verLineView.frame = CGRectMake(CGRectGetMaxX(cancleBtn.frame), CGRectGetMaxY(lineView.frame), 1, 40);
- verLineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
- [alertView addSubview:verLineView];
- self.verLineView = verLineView;
-
-
- UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
- sureBtn.frame = CGRectMake(CGRectGetMaxX(verLineView.frame), CGRectGetMaxY(lineView.frame), (AlertW-1)/2+1, 40);
- [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
- [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
- [sureBtn setTitle:sureTitle forState:UIControlStateNormal];
- sureBtn.tag = 2;
- [sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- // UIBezierPath *bmaskPath = [UIBezierPath bezierPathWithRoundedRect:sureBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
- // CAShapeLayer *bmaskLayer = [[CAShapeLayer alloc] init];
- // maskLayer.frame = sureBtn.bounds;
- // maskLayer.path = bmaskPath.CGPath;
- // sureBtn.layer.mask = bmaskLayer;
- //sureBtn.layer.masksToBounds = YES;
- [alertView addSubview:sureBtn];
- self.sureBtn = sureBtn;
- }
-
- /*if (cancleTitle && sureTitle) {
-
- }*/
-
- //只有取消按钮
- if (cancleTitle && !sureTitle) {
-
- UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
- cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame), AlertW, 40);
- [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
- [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
- [cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
- //[self.cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
- cancleBtn.tag = 1;
- [cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
- CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
- maskLayer.frame = cancleBtn.bounds;
- maskLayer.path = maskPath.CGPath;
- cancleBtn.layer.mask = maskLayer;
-
- [self.alertView addSubview:cancleBtn];
- self.cancleBtn = cancleBtn;
- }
-
- //只有确定按钮
- if(sureTitle && !cancleTitle){
-
- UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
- sureBtn.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame), AlertW, 40);
- [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
- [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
- [sureBtn setTitle:sureTitle forState:UIControlStateNormal];
- //[self.sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
- sureBtn.tag = 2;
- [sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
- CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
- maskLayer.frame = self.sureBtn.bounds;
- maskLayer.path = maskPath.CGPath;
- self.sureBtn.layer.mask = maskLayer;
-
- [self.alertView addSubview:sureBtn];
- self.sureBtn = sureBtn;
-
- }
-
- //计算高度
- CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
- alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
- alertView.layer.position = self.center;
-
- [self registerForKeyboardNotifications];
- }
-
- return self;
- }
- #pragma mark - 弹出 -
- - (void)showXBAlertView
- {
- UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
- [rootWindow addSubview:self];
- [self creatShowAnimation];
- }
- - (void)creatShowAnimation
- {
- self.alertView.layer.position = self.center;
- self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
- [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
- self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
- } completion:^(BOOL finished) {
- }];
- }
- #pragma mark - 回调 -设置只有2 -- > 确定才回调
- - (void)buttonEvent:(UIButton *)sender
- {
- if (sender.tag == 2) {
- if (self.alertresult) {
- [self.textField resignFirstResponder];
- [self.actityindicatorview startAnimating];
- sender.enabled = NO;
- self.alertresult(self.textField.text);
- }
- }
- else{
- [self removeFromSuperview];
- }
- //
- }
- -(void)showErrorMsg:(NSString *)errormsg{
- self.msgLbl.text = errormsg;
-
- }
- -(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andIsTitle:(BOOL)isTitle
- {
- UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
- contentLbl.numberOfLines = 0;
- contentLbl.text = contentStr;
- contentLbl.textAlignment = NSTextAlignmentCenter;
- if (isTitle) {
- contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
- }else{
- contentLbl.font = [UIFont systemFontOfSize:14.0];
- }
-
- NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
- NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStyle alloc] init];
- mParaStyle.lineBreakMode = NSLineBreakByCharWrapping;
- [mParaStyle setLineSpacing:3.0];
- [mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0,[contentStr length])];
- [contentLbl setAttributedText:mAttrStr];
- [contentLbl sizeToFit];
-
- return contentLbl;
- }
- -(UIImage *)imageWithColor:(UIColor *)color
- {
- CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
- UIGraphicsBeginImageContext(rect.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [color CGColor]);
- CGContextFillRect(context, rect);
- UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return theImage;
- }
- -(void)startActity{
- [self.actityindicatorview startAnimating];
- }
- -(void)stopActityAndRemoveAlertView{
- [self.actityindicatorview stopAnimating];
- self.sureBtn.enabled = YES;
- [self removeFromSuperview];
- }
- //针对键盘 frame调整
- - (void)registerForKeyboardNotifications
- {
- //使用NSNotificationCenter 鍵盤出現時
- [[NSNotificationCenter defaultCenter] addObserver:self
-
- selector:@selector(keyboardWasShown:)
-
- name:UIKeyboardWillShowNotification object:nil];
-
- //使用NSNotificationCenter 鍵盤隐藏時
- [[NSNotificationCenter defaultCenter] addObserver:self
-
- selector:@selector(keyboardWillBeHidden:)
-
- name:UIKeyboardWillHideNotification object:nil];
-
- }
- - (void)keyboardWasShown:(NSNotification*)aNotification
- {
- NSDictionary* ainfo = [aNotification userInfo];
- CGSize akbSize = [[ainfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
- NSNumber *aduration = [ainfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
- __weak typeof(self.alertView)weakalertview = self.alertView;
- __weak typeof(self)weakSelf = self;
- [UIView animateWithDuration:aduration.doubleValue animations:^{
- //判断键盘是退出还是出现.
- weakalertview.layer.position = CGPointMake(weakalertview.layer.position.x,(weakSelf.bounds.size.height- akbSize.height)/2);
- }];
- }
- - (void)keyboardWillBeHidden:(NSNotification*)aNotification
- {
- NSDictionary* ainfo = [aNotification userInfo];
-
- NSNumber *aduration = [ainfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
- __weak typeof(self.alertView)weakalertview = self.alertView;
- __weak typeof(self)weakSelf = self;
- [UIView animateWithDuration:aduration.doubleValue animations:^{
- weakalertview.layer.position = weakSelf.center;
-
-
- }];
- }
- -(void)dealloc{
- [[NSNotificationCenter defaultCenter]removeObserver:self];
- NSLog(@"delloc xbalertview");
- }
- @end
|