iOS-自定義AlertView

使用工廠方法創(chuàng)建2種類型的自定義AlertView。

效果圖:


類似系統(tǒng)的AlertView
豎直菜單欄

在.h文件中實現:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@protocol OZAlertViewDelegate <NSObject>

@optional
// 點擊按鈕下標時傳遞參數
- (void)didSelectVerticalAlertButton:(NSString *)title;
- (void)didSelectHorizontalAlertButton:(NSString *)title;
@end

@interface OZAlertView : NSObject

@property (nonatomic,weak) id <OZAlertViewDelegate> delegate;

/**
 *單例
 */
+ (instancetype)shareInstance;

/**
 *快速豎直創(chuàng)建提示框
 */
- (UIView *)quickVerticalAlertViewWithArray:(NSArray *)array;

/**
 *快速橫向創(chuàng)建提示框
 */
- (UIView *)quickHorizontalAlertViewTitle:(NSString *)title Content:(NSString *)content ButtonTitles:(NSArray *)buttonTitles;
@end

在.m文件中實現

#import "OZAlertView.h"
#import "OZHelper.h"

@implementation OZAlertView

+ (instancetype)shareInstance {
    static OZAlertView *alert;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alert = [[OZAlertView alloc] init];
    });
    return alert;
}

- (UIView *)quickVerticalAlertViewWithArray:(NSArray *)array {
    
    UIView *groundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    groundView.alpha = 0;
    groundView.hidden = YES;
    groundView.backgroundColor = [UIColor clearColor];
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.5;
    [groundView addSubview:backgroundView];
    
    CGFloat buttonH = 61;
    CGFloat buttonW = 250;
    
    // 通過數組長度創(chuàng)建view的高
    UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0,buttonW, array.count*buttonH)];
    // 切圓角
    alert.layer.masksToBounds = YES;
    alert.layer.cornerRadius = 10;
    alert.center = groundView.center;
    [groundView addSubview:alert];
    
    for (int i = 0; i < array.count; i++) {
        // 因為有一條分割線 所以最下面是一層view
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i*buttonH, buttonW, buttonH)];
        view.backgroundColor = [UIColor whiteColor];
        
        //創(chuàng)建button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, 0, buttonW, buttonH);
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        
        [button addTarget:self action:@selector(verticalAlertAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
        CGFloat height = [OZHelper heightOfString:array[0] font:[UIFont systemFontOfSize:17] width:buttonW - 20];
        
        //設置修改Frame
        if (height > buttonH) {
            if (i == 0) {
                button.frame = CGRectMake(10, 10, buttonW - 20, height);
                
                CGRect viewFrame = view.frame;
                viewFrame.size.height = 20 + height;
                view.frame = viewFrame;
                
                CGRect alertFrame = alert.frame;
                alertFrame.size.height += view.frame.size.height - buttonH;
                alert.frame = alertFrame;
            }
            else
            {
                CGRect viewFrame = view.frame;
                viewFrame.origin.y = 20 + height;
                view.frame = viewFrame;
            }
        }
        
        //設置第一行不能點擊
        if (i == 0) {
            button.userInteractionEnabled = NO;
            button.titleLabel.numberOfLines = 0;
        }
        
        //設置顏色分割線
        if (i == array.count - 1) {
            button.tintColor = [UIColor whiteColor];
            // 背景
            view.backgroundColor = [UIColor purpleColor];
        }
        else {
            button.tintColor = [UIColor purpleColor];
            // 分割線
            // 如果不是最后一行
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, buttonW, 1)];
            lineView.backgroundColor = [UIColor purpleColor];
            [view addSubview:lineView];
        }
        [alert addSubview:view];
    }
    
    return groundView;
}

- (UIView *)quickHorizontalAlertViewTitle:(NSString *)title Content:(NSString *)content ButtonTitles:(NSArray *)buttonTitles
{
    UIView *groundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    groundView.alpha = 0;
    groundView.hidden = YES;
    groundView.backgroundColor = [UIColor clearColor];
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.5;
    [groundView addSubview:backgroundView];
    
    CGFloat buttonH = 61;
    CGFloat buttonW = 125;
    
    CGFloat height = [OZHelper heightOfString:content font:[UIFont systemFontOfSize:15] width:2*buttonW - 20] + 23;
    
    // 通過數組設置創(chuàng)建view的高
    CGFloat alertHeight = height > buttonH ? 2*buttonH + height - buttonH + 20 : 2*buttonH;
    UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0,2*buttonW, alertHeight)];
    // 設置圓角
    alert.layer.masksToBounds = YES;
    alert.layer.cornerRadius = 10;
    alert.center = groundView.center;
    [groundView addSubview:alert];
    
    for (int i = 0; i < 2; i++) {
        // 因為有一條分割線 所以最下面是一層view
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        
        if (i == 0) {
            
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 2*buttonW - 20, 20)];
            titleLabel.text = title;
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.textColor = [UIColor purpleColor];
            titleLabel.font = [UIFont boldSystemFontOfSize:17];
            titleLabel.backgroundColor = [UIColor whiteColor];
            [view addSubview:titleLabel];
            
            UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame), 2*buttonW - 20, height)];
            contentLabel.text = content;
            contentLabel.font = [UIFont systemFontOfSize:15];
            contentLabel.textColor = [UIColor purpleColor];
            contentLabel.numberOfLines = 0;
            contentLabel.backgroundColor = [UIColor whiteColor];
            [view addSubview:contentLabel];
            
            view.frame = CGRectMake(0, 0, 2*buttonW, CGRectGetMaxY(contentLabel.frame)+3);
        }
        else
        {
            for (int j = 0; j < buttonTitles.count; j++) {
                //創(chuàng)建button
                UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
                button.frame = CGRectMake(j*buttonW, 0, buttonW, buttonH);
                [button setTitle:buttonTitles[j] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:17];
                [button addTarget:self action:@selector(horizontalAlertAction:) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:button];
                
                // 這里可以根據傳值改變狀態(tài)
                if (j == 0) {
                    button.tintColor = [UIColor whiteColor];
                    button.backgroundColor = [UIColor purpleColor];
                }
                else {
                    button.tintColor = [UIColor purpleColor];

                    // 分割線
                    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(buttonW, 0, buttonW, 1)];
                    lineView.backgroundColor = [UIColor purpleColor];
                    [view addSubview:lineView];
                }
            }
            
            view.frame = CGRectMake(0, height>buttonH?height:buttonH, 2*buttonW, buttonH);
        }
        [alert addSubview:view];
    }
    return groundView;
}

- (void)verticalAlertAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(didSelectVerticalAlertButton:)]) {
        [self.delegate didSelectVerticalAlertButton:sender.currentTitle];
    }
}

- (void)horizontalAlertAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(didSelectHorizontalAlertButton:)]) {
        [self.delegate didSelectHorizontalAlertButton:sender.currentTitle];
    }
}

@end

然后在ViewController中實現:

#import "ViewController.h"
#import "OZAlertView.h"

static NSString *const Title = @"Hellow World~";
static NSString *const content = @"Hellow World~Hellow World~";
static NSString *const btnTitle1 = @"btn1";
static NSString *const btnTitle2 = @"btn2";
static NSString *const btnTitle3 = @"btn3";

@interface ViewController () <OZAlertViewDelegate>
@property (nonatomic, strong) UIView *alert;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //[self createOZHorizontalAlertView];
    [self createOZVerticalAlertView];
    [self alertViewShow];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - OZAlertViewDelegate
- (void)didSelectHorizontalAlertButton:(NSString *)title {
    [self alertViewHide];
}

- (void)didSelectVerticalAlertButton:(NSString *)title {
    [self alertViewHide];
}

#pragma mark -
- (void)createOZHorizontalAlertView {
    self.alert = [[OZAlertView shareInstance] quickHorizontalAlertViewTitle:Title
                                                                    Content:content
                                                               ButtonTitles:@[btnTitle1,btnTitle2]];
    [self.view addSubview:self.alert];
}

- (void)createOZVerticalAlertView {
    self.alert = [[OZAlertView shareInstance] quickVerticalAlertViewWithArray:@[content,btnTitle1,btnTitle2,btnTitle3]];
    
    [self.view addSubview:self.alert];
}

- (void)alertViewShow {
    self.alert.hidden = NO;
    [UIView animateWithDuration:0.35 animations:^{
        self.alert.alpha = 1;
    }];
}

- (void)alertViewHide {
    [UIView animateWithDuration:0.35 animations:^{
        self.alert.alpha = 0;
    } completion:^(BOOL finished) {
        self.alert.hidden = YES;
        [self.alert removeFromSuperview];
    }];
}

@end

Demo地址:https://github.com/olivierzh/OZAlertView.git

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容