iOS 自定义弹框

项目中需要自定义弹框就撸一个弹框, 做个记录。

QQ20170626-113554.png

xib 自定义一个View,添加在keyWindow上即可,block绑定点击按钮之后的回调。

#import <UIKit/UIKit.h>

@interface LXAlertView : UIView
-(instancetype)initWithTitle:(NSString *)title
                leftBtnTitle:(NSString *)leftBtnTitle
                 rightBtnTitle:(NSString *)rightBtnTitle
                 alertResult:(void (^)(NSInteger index)) alertResult;


-(void)showLXAlertView;
@end
//
//  LXAlertView.m
//  LXAlertview
//
//  Created by �zhongzhi on 2017/6/22.
//  Copyright © 2017年 �zhongzhi. All rights reserved.
//

#import "LXAlertView.h"
#import "LXCustomAlert.h"
typedef void(^alertResult) (NSInteger index);
@interface LXAlertView ()
@property(nonatomic,strong)LXCustomAlert *customAlert;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *leftBtnTitle;
@property(nonatomic,copy)NSString *rightBtnTitle;
@property(nonatomic,copy)alertResult alertResult;

@end
@implementation LXAlertView

-(instancetype)initWithTitle:(NSString *)title leftBtnTitle:(NSString *)leftBtnTitle rightBtnTitle:(NSString *)rightBtnTitle alertResult:(void (^)(NSInteger index)) alertResult;{
    
    self = [super init];
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
    
        self.backgroundColor = [LBColor(98, 87, 56)colorWithAlphaComponent:0.9];
        
        
        self.title = title;
        
        self.leftBtnTitle = leftBtnTitle;
        
        self.rightBtnTitle = rightBtnTitle;
        
        self.alertResult = alertResult;
        
        [self setup];
    }
    return self;
    
}
-(void)showLXAlertView{
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:self];
    
    [self creatShowAnimation];
}

-(void)setup{
    
    [self addSubview:self.customAlert];
    
    self.customAlert.title = self.title;
    
    self.customAlert.leftBtnTitle = self.leftBtnTitle;
    
    self.customAlert.rightBtnTitle = self.rightBtnTitle;
    
    self.customAlert.leftBtn.tag = 1;
    
    self.customAlert.rightBtn.tag = 2;
    
    [self.customAlert.leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.customAlert.rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
}


-(void)btnClick:(UIButton *)button
{
    
    
        
    if (self.alertResult) {
            
        
        [self dismiss];
                
        
        self.alertResult(button.tag);

 
    }
    [self removeFromSuperview];
}

- (void)creatShowAnimation
{
    
    self.customAlert.transform = CGAffineTransformMakeScale(0.90, 0.90);
    
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.customAlert.transform = CGAffineTransformMakeScale(1.0, 1.0);
        
    } completion:^(BOOL finished) {
        
    }];
}
-(void)dismiss{
    self.customAlert.transform = CGAffineTransformMakeScale(1.0, 1.0);
    
    [UIView animateWithDuration:0.20 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.customAlert.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}

-(LXCustomAlert *)customAlert{
    if (!_customAlert) {
        
        _customAlert =[[NSBundle mainBundle]loadNibNamed:@"LXCustomAlert" owner:self options:nil].firstObject;
        
        _customAlert.frame = CGRectMake(Device_Width/2 -140, (Device_Height -64 - 145)/2, 280, 145);
    }
    
    return _customAlert;
}
@end

demo地址:LXAlertView

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言: 在开发会遇到众多自定义弹框的需求,如果是用到的情况非常多的话,就有有必要创建一个跟控制器,再根控制器中实现...
    Mr_Bob_阅读 8,540评论 1 8
  • 根据需求文档,要实现一个弹框选择器,本来想在网上找一个人家写好的拿来用用的,但是最近收到多方打击,决定动手写一个。...
    泥_叔阅读 7,934评论 0 8
  • 一个可以自定义弹出视图内容,弹出视图所在位置的小轮子。 使用:
    劉光軍_MVP阅读 6,747评论 3 6
  • https://github.com/wangkecheng/WSAlertActionView
    WARRON阅读 3,177评论 0 0
  • 佛教心理学的老师说:当作家不容易,你看他们往往写不出生动的伟人,而那些小人却被写活了。因为他们自己不是伟人。三国演...
    HRH阅读 1,124评论 0 0