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_閱讀 3,333評論 1 8
  • 根據需求文檔,要實現一個彈框選擇器,本來想在網上找一個人家寫好的拿來用用的,但是最近收到多方打擊,決定動手寫一個。...
    泥_叔閱讀 2,942評論 0 8
  • 一個可以自定義彈出視圖內容,彈出視圖所在位置的小輪子。 使用:
    劉光軍_MVP閱讀 2,311評論 3 6
  • https://github.com/wangkecheng/WSAlertActionView
    WARRON閱讀 455評論 0 0
  • 佛教心理學的老師說:當作家不容易,你看他們往往寫不出生動的偉人,而那些小人卻被寫活了。因為他們自己不是偉人。三國演...
    HRH閱讀 162評論 0 0