alertview样式3(带输入框 加 减 确认 操作 含键盘遮挡处理)

.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ENToothIntervalAlertView : UIView
@property (nonatomic, copy) void (^sureBlock)(NSString *space);
@property (nonatomic, copy) void (^dismissBlock)(void);
- (void)show;
//定制弹窗
- (id)initWithSpace:(NSString *)space;
@end

NS_ASSUME_NONNULL_END

.m文件

#import "ENToothIntervalAlertView.h"
#import "XWUtils.h"
#import "UITextField+PaddingLeft.h"
#import "IQKeyboardManager.h"
#define WIDTHRADIUS_6 MIN(SCREEN_WIDTH, SCREEN_HEIGHT)/375.0
#define kAlertWidth WIDTHRADIUS_6*240.f
#define kAlertHeight WIDTHRADIUS_6*54.f
@interface ENToothIntervalAlertView ()<UIGestureRecognizerDelegate,UITextFieldDelegate>
{
    BOOL _leftLeave;
}
@property (nonatomic, strong) UIView        *backImageView;
@property (nonatomic, strong) UIView        *vBG;//视图
@property (nonatomic, strong) UIView        *vInputBg;//输入的背景view
@property (nonatomic, strong) UIButton      *addBtn;
@property (nonatomic, strong) UIButton      *minusBtn;
@property (nonatomic, strong) UITextField   *tfNum;//数字
@property (nonatomic, strong) UILabel       *lblDesc;//mm
@property (nonatomic, strong) UIButton      *sureBtn;
@property (nonatomic, strong) UIButton      *closeBtn;
@end
@implementation ENToothIntervalAlertView
- (id)initWithSpace:(NSString *)space
{
    self = [super init];
    if (self) {
        // 键盘出现的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
        // 键盘消失的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHiden:) name:UIKeyboardWillHideNotification object:nil];
        [self addSubview:self.vBG];
        [self.vBG addSubview:self.vInputBg];
        [self.vInputBg addSubview:self.addBtn];
        [self.vInputBg addSubview:self.tfNum];
        [self.vInputBg addSubview:self.minusBtn];
        [self.vBG addSubview:self.lblDesc];
        [self.vBG addSubview:self.sureBtn];
        [self.vBG addSubview:self.closeBtn];
        [self setNeedsUpdateConstraints];
        [self obtainSpace:space];
    }
    return self;
}
- (void)dealloc{
}
- (void)updateConstraints {
    [super updateConstraints];
    [self.vBG mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.vInputBg mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.vBG).offset(15*WIDTHRADIUS_6);
        make.centerY.equalTo(self.vBG.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(120*WIDTHRADIUS_6, 26*WIDTHRADIUS_6));
    }];
    [self.minusBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.vInputBg.mas_left);
        make.centerY.equalTo(self.vInputBg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(25*WIDTHRADIUS_6, 26*WIDTHRADIUS_6));
    }];
    [self.tfNum mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.minusBtn.mas_left).offset(0);
        make.centerY.equalTo(self.vInputBg.mas_centerY);
        make.centerX.equalTo(self.vInputBg.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(70*WIDTHRADIUS_6, 26*WIDTHRADIUS_6));
    }];
    [self.addBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.vInputBg.mas_right);
        make.centerY.equalTo(self.vInputBg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(25*WIDTHRADIUS_6, 26*WIDTHRADIUS_6));
    }];
    [self.lblDesc mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.vInputBg.mas_right).offset(5*WIDTHRADIUS_6);
        make.centerY.equalTo(self.vBG.mas_centerY);
    }];
    [self.sureBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.vBG.mas_right).offset(-12*WIDTHRADIUS_6);
        make.centerY.equalTo(self.vBG.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(23*WIDTHRADIUS_6, 22*WIDTHRADIUS_6));
    }];
    [self.closeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.sureBtn.mas_left).offset(-10*WIDTHRADIUS_6);
        make.centerY.equalTo(self.vBG.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(23*WIDTHRADIUS_6, 22*WIDTHRADIUS_6));
    }];
}
-(void)obtainSpace:(NSString *)space{
    self.tfNum.text = space;
}
- (void)show {
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    UIViewController *topVC = [self appRootViewController];
//    self.vBG.frame = CGRectMake(0,14*WIDTHRADIUS, kAlertWidth, _contentHeight+256*WIDTHRADIUS-(!self.isChose?23*WIDTHRADIUS:0));
    self.frame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(topVC.view.bounds) - kAlertHeight) * 0.5, kAlertWidth, kAlertHeight);
    [self exChangeOut:self dur:0.4f];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)hide:(UIButton *)sender {
    [self removeFromSuperviews];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}
//完全自适应alertView
-(void)clickBtnClose{
    _leftLeave = YES;
    [self hide:nil];
}
-(void)clickBtnSure{
    _leftLeave = YES;
    [self hide:nil];
    NSString *strText =[NSString stringWithFormat:@"%.1f",[self.tfNum.text floatValue]];
    if ([strText floatValue] > 10) {
        strText = @"10.0";
    }
    if (self.tfNum.text.length == 0) {
        strText = @"0.0";
    }
    !self.sureBlock?:self.sureBlock(DSStringValue(strText));
}
#pragma mark 键盘事件
//键盘展示
-(void)keyboardWasShown:(NSNotification *)notification{
    UIViewController *topVC = [self appRootViewController];
    CGRect rect = self.frame;
    rect.origin.y = (CGRectGetHeight(topVC.view.bounds) - 54*WIDTHRADIUS_6) * 0.5-90*WIDTHRADIUS_6;
    self.frame = rect;
}
//键盘隐藏
-(void)keyboardWillBeHiden:(NSNotification *)notification{
    if (self.tfNum.text.length == 0) {
        self.tfNum.text = @"0.0";
    }
    UIViewController *topVC = [self appRootViewController];
    CGRect rect = self.frame;
    rect.origin.y = (CGRectGetHeight(topVC.view.bounds) - 54*WIDTHRADIUS_6) * 0.5;
    self.frame = rect;
}
//结束编辑判断数值
- (void)textFieldDidEndEditing:(UITextField *)textField{
    textField.text = [NSString stringWithFormat:@"%.2f",[textField.text floatValue]];
    if ([textField.text floatValue] > 10) {
        textField.text = @"10.0";
    }
    if (textField.text.length == 0) {
        textField.text = @"0.0";
    }
}

- (UIViewController *)appRootViewController {
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

- (void)removeFromSuperviews {
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    UIViewController *topVC = [self appRootViewController];
    //_isVideoAnswer 视频问答弹窗
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth) * 0.5, CGRectGetHeight(topVC.view.bounds), kAlertWidth, kAlertHeight);
    [UIView animateWithDuration:0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        if (self->_leftLeave) {
            self.transform = CGAffineTransformMakeRotation(-M_1_PI / 1.5);
        }else {
            self.transform = CGAffineTransformMakeRotation(M_1_PI / 1.5);
        }
    } completion:^(BOOL finished) {
        //注释原因:DXAlertView弹框切后台会在这crash
        [self removeFromSuperview];
    }];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (newSuperview == nil) {
        return;
    }
    UIViewController *topVC = [self appRootViewController];
    
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:topVC.view.bounds];
        self.backImageView.backgroundColor = [UIColor blackColor];
        self.backImageView.alpha = 0.6f;
        self.backImageView.userInteractionEnabled = YES;
        self.backImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.backImageView];
    self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(topVC.view.bounds) - kAlertHeight) * 0.5, kAlertWidth, kAlertHeight);
    [UIView animateWithDuration:0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.transform = CGAffineTransformMakeRotation(0);
        self.frame = afterFrame;
    } completion:^(BOOL finished) {
    }];
    [super willMoveToSuperview:newSuperview];
}

- (void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.08, 1.08, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
}

- (void)cancle {
    [self hide:nil];
}
-(void)clickBtnAddMinus:(UIButton*)btn{//增减
    CGFloat spaceNum = [self.tfNum.text floatValue];
    if (btn.tag == 1) {
        if (spaceNum >= 0.1) {
            spaceNum = spaceNum - 0.1;
        }else if (spaceNum == 0){
            spaceNum = 0.0;
        }
    }else if (btn.tag == 2){
        spaceNum = spaceNum + 0.1;
    }
    self.tfNum.text = [NSString stringWithFormat:@"%.1f",spaceNum];
}
-(UIView *)vBG{
    if (!_vBG) {
        _vBG = [XWTool viewbackground:Color_White SuperView:nil];
    }
    return _vBG;
}
-(UIView *)vInputBg{
    if (!_vInputBg) {
        _vInputBg = [XWTool viewbackground:Color_White SuperView:nil];
        _vInputBg.layer.cornerRadius = 4*ScreenWithRateFor6Land;
        _vInputBg.layer.masksToBounds = YES;
        _vInputBg.layer.borderColor = Color_Common_Red.CGColor;
        _vInputBg.layer.borderWidth = 1;
    }
    return _vInputBg;
}
-(UITextField *)tfNum{
    if (!_tfNum) {
        _tfNum = [[UITextField alloc]init];
        _tfNum.textAlignment = NSTextAlignmentCenter;
        _tfNum.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _tfNum.font = REGULAR_FONTLAND(12);
        _tfNum.textColor = Color_Title;
    }
    return _tfNum;
}
- (UILabel *)lblDesc {
    if (!_lblDesc) {
        _lblDesc = [XWTool lblTitile:NSLocalizedString(@"space_mm", nil) font:12 textColor:Color_Title SuperView:nil];
    }
    return _lblDesc;
}
-(UIButton *)minusBtn{
    if (!_minusBtn) {
        _minusBtn = [XWTool btnImage:ImageNamed(@"tooth_minus") SuperView:nil];
        _minusBtn.backgroundColor = UIColorFromRGB(0xF4F4F6);
        _minusBtn.tag = 1;
        [_minusBtn addTarget:self action:@selector(clickBtnAddMinus:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _minusBtn;
}
-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [XWTool btnImage:ImageNamed(@"tooth_add") SuperView:nil];
        _addBtn.backgroundColor = UIColorFromRGB(0xF4F4F6);
        _addBtn.tag = 2;
        [_addBtn addTarget:self action:@selector(clickBtnAddMinus:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}
-(UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [XWTool btnImage:ImageNamed(@"tooth_sure") SuperView:nil];
        [_sureBtn addTarget:self action:@selector(clickBtnSure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
-(UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [XWTool btnImage:ImageNamed(@"tooth_close") SuperView:nil];
        [_closeBtn addTarget:self action:@selector(clickBtnClose) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
@end

猜你喜欢

转载自blog.csdn.net/weixin_42050662/article/details/122734421