iOS自定义警告框(OC)

本文参考Demo:http://code4app.com/ios/JKAlertDialog/55019c35933bf078768b4748

如果在导航中使用自定义警告框,如果警告框添加在view上的话则导航栏不会被挡住,只有添加在window上才能挡住导航栏

这里主要对如何自定义警告框进行一个介绍

首先在CustomAlertView.h中写

#import 

@interface CustomAlertView : UIView

- (id)initWithFrame:(CGRect)frame withTitleStr:(NSString *)titleStr withContentStr:(NSString *)contentStr withButtonStr:(NSString *)buttonStr;
- (void)show;//警告框展示的方法

@end
在CustomAlertView.m中写

@implementation CustomAlertView
- (id)initWithFrame:(CGRect)frame withTitleStr:(NSString *)titleStr withContentStr:(NSString *)contentStr withButtonStr:(NSString *)buttonStr
{
    self = [super initWithFrame:frame];
    if (self) {
        _titleStr = titleStr;
        _contentStr = contentStr;
        _buttonStr = buttonStr;
        [self creatView];
    }
    return self;
}

- (void)creatView
{
    _subBackView = [[UIView alloc] initWithFrame:self.frame];
    _subBackView.backgroundColor = [UIColor blackColor];
    _subBackView.alpha = 0;
    [self addSubview:_subBackView];
    
    UIView *backView = [[UIView alloc] init];
    backView.center = CGPointMake(self.frame.size.width / 2.0f, self.frame.size.height / 2.0 - 50);
    backView.bounds = CGRectMake(0, 0, 250, 185);
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.cornerRadius = 5;
    backView.layer.masksToBounds = YES;
    [self addSubview:backView];
    _backView = backView;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 15)];
    titleLabel.text = _titleStr;
    titleLabel.font = [UIFont systemFontOfSize:13.0f];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor grayColor];
    [backView addSubview:titleLabel];
    
    UITextView *contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 25, CGRectGetWidth(backView.frame) - 10, 110)];
    contentTextView.text = _contentStr;
    contentTextView.font = [UIFont systemFontOfSize:12.0f];
    contentTextView.backgroundColor = [UIColor clearColor];
    contentTextView.delegate = self;
    contentTextView.textColor = [UIColor grayColor];
    [backView addSubview:contentTextView];
    
    UIButton *certainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    certainButton.frame = CGRectMake(20, 140, 210, 40);
    certainButton.backgroundColor = [UIColor purpleColor];
    certainButton.layer.cornerRadius = 5;
    certainButton.layer.masksToBounds = YES;
    certainButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    certainButton.titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
    [certainButton setTitle:_buttonStr forState:UIControlStateNormal];
    [certainButton addTarget:self action:@selector(certainButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:certainButton];
}

#pragma mark - certainButtonClick
- (void)certainButtonClick
{
    [UIView animateWithDuration:0.4 animations:^{
        _backView.alpha = 0.0;
        _subBackView.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - show
-(UIView*)topView{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    return  window.subviews[0];
}

- (void)show
{
    [UIView animateWithDuration:0.5 animations:^{
        _backView.alpha = 1;
        _subBackView.alpha = 0.5;
        
    } completion:^(BOOL finished) {
        
    }];
    [[self topView] addSubview:self];
    [self showAnimation];
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return NO;
}

#pragma mark - Animation
- (void)showAnimation {
    CAKeyframeAnimation *alertViewshowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    alertViewshowAnimation .duration = 0.4;
    alertViewshowAnimation .values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    alertViewshowAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f];
    alertViewshowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [_backView.layer addAnimation:showAnimation forKey:nil];
}

@end

使用方法

    CustomAlertView *rulesView = [[CustomAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds withTitleStr:@"获取规则" withContentStr:@"1.根据投资次数:投资一次获得1分,每天获取1次;\n2.根据投资次数:投资一次获得1分,每天获取1次;\n3.根据投资次数:投资一次获得1分,每天获取1次;" withButtonStr:@"我知道了"];
    [rulesView show];
效果图
iOS自定义警告框(OC)_第1张图片

你可能感兴趣的:(iOS自定义警告框(OC))