美文网首页IOSiOS开发专题攻城狮
iOS-自定义AlertView(便利构造器)

iOS-自定义AlertView(便利构造器)

作者: 轩辕小羽 | 来源:发表于2015-12-05 16:26 被阅读2445次

前言


遍历构造器又称工厂方法,可以把一个或多个控件封装到一个类中,每次创建控件只需要调用方法就可以了

本次我所说的就是封装一个根据所输入的数组进行自动创建提示框的类

效果图:

这里写图片描述
这里写图片描述

上代码

首先创建一个CustomAlertView的类,该类继承自NSobject

这里写图片描述

然后在CustomAlertView.h中写上方法声明,因为是继承自NSobject所以要手动导入UIKit框架

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

// 代理方法
@protocol CustomAlertViewDelegate <NSObject>

// 可选执行方法
@optional
// 点击按钮下标时传递参数
- (void)didSelectAlertButton:(NSString *)title;
@end

@interface CustomAlertView : NSObject
/** 单例 */
+ (CustomAlertView *)singleClass;

/** 快速创建提示框*/
- (UIView *)quickAlertViewWithArray:(NSArray *)array;

// 代理属性
@property (assign, nonatomic)id<CustomAlertViewDelegate>delegate;

@end

然后在CustomAlertView.h.m中开始实现方法

#import "CustomAlertView.h"

/** 二进制码转RGB */
#define UIColorFromRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@implementation CustomAlertView
/** 单例 */
+ (CustomAlertView *)singleClass{
    static CustomAlertView *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CustomAlertView alloc] init];
    });
    return manager;
}
/** 提示view */
- (UIView *)quickAlertViewWithArray:(NSArray *)array{
    CGFloat buttonH = 61;
    CGFloat buttonW = 250;

    // 通过数组长度创建view的高
    UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0,buttonW, array.count * buttonH)];
    for (int i = 0; i < array.count;i++) {
        // 因为有一条分割线 所以最下面是一层view
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i*buttonH, buttonW, buttonH)];
        view.backgroundColor = [UIColor whiteColor];
        
        // 创建button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, 0, buttonW, buttonH);
        [button setTitle:array[i] forState:(UIControlStateNormal)];
        // 所有button都关联一个点击方法,通过按钮上的title做区分
        [button addTarget:self action:@selector(alertAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view addSubview:button];
        
        // 这里可以根据传值改变状态
        if ([array[i] isEqualToString:@"取消"]) {
            button.tintColor = [UIColor whiteColor];
            // 绿色背景
            view.backgroundColor = UIColorFromRGBValue(0x82DFB0);
        }else{
            button.tintColor = UIColorFromRGBValue(0x333333);
            // 分割线
            // 如果不是最后一行
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, buttonW, 1)];
            lineView.backgroundColor = UIColorFromRGBValue(0xefefef);
            [view addSubview:lineView];
        }
        [alert addSubview:view];
    }
    return alert;
}
/** button点击事件,通知代理执行代理方法 */
- (void)alertAction:(UIButton *)button{
    [_delegate didSelectAlertButton:button.titleLabel.text];
}
@end

ViewController中调用

#import "ViewController.h"
#import "CustomAlertView.h"

@interface ViewController ()<CustomAlertViewDelegate>
/** 提示框 */
@property (strong, nonatomic) UIView *alertView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor lightGrayColor];
    // 将提示页面加入到view上
    [self.view addSubview:self.alertView];
}

// 这个是stroyBoard里创建的button
- (IBAction)alertAction:(UIButton *)sender {
    // UIView动画
    [UIView animateWithDuration:0.1 animations:^{
        self.alertView.alpha = 1;
        self.alertView.hidden = NO;
    }];
}

/** 提示框懒加载 */
- (UIView *)alertView{
    if (!_alertView) {
        // 这里还可以把alerView创建到一个蒙版上,直接进行操作蒙版的透明度隐藏来展示动画,也可以避免点击框外的其他控件,就不在这里细写了
        // 赋值
        _alertView = [[CustomAlertView singleClass]
                      // 传入数组
                      quickAlertViewWithArray:@[@"确定",@"测试A",@"测试B",@"取消"]
                      ];
        
        // 设定中心,如果需要适配请layoutIfNeed
        _alertView.center = self.view.center;
        // 切圆角
        _alertView.layer.masksToBounds = YES;
        _alertView.layer.cornerRadius = 10;
        // 初始状态为隐藏,透明度为0
        _alertView.hidden = YES;
        _alertView.alpha = 0.0;
        // 设置代理
        [CustomAlertView singleClass].delegate = self;
    }
    return _alertView;
}

// 代理方法传值
- (void)didSelectAlertButton:(NSString *)title{
    [UIView animateWithDuration:0.1 animations:^{
        self.alertView.alpha = 0;
    } completion:^(BOOL finished) {
        // 如果直接在动画里隐藏不会出现动画效果,所以要在动画结束之后进行隐藏
        self.alertView.hidden = YES;
    }];
    NSLog(@"%@",title);
}

@end

来张效果图

这里写图片描述

以上就是本功能的全部代码

GitHub:https://github.com/Lafree317/customAlertView

相关文章

  • iOS-自定义AlertView(便利构造器)

    前言 遍历构造器又称工厂方法,可以把一个或多个控件封装到一个类中,每次创建控件只需要调用方法就可以了 本次我所说的...

  • UIViewController

    初始化 ① 自定义指定构造器方法 ② 便利构造器方法

  • Swift 定义枚举类

    添加构造器 便利构造器

  • 构造方法总结二

    构造器间的调用规则 指定构造器必须调用其直接父类的"指定构造器" 便利构造器必须调用同类中的其它构造器(指定或便利...

  • IOS 日常随笔

    自己的demo 自定义Alertview -- LSAlertView图片浏览器 -- LSShowImgs倒...

  • Swift5 构造函数知识点总结

    Swift 为类提供了两种构造器,分别是指定构造器和便利构造器。 指定构造器必须总是向上代理(重写、重载) 便利构...

  • Swift初始化init中的一些坑

    自定义控件初始化中常见的几种错误(指定构造器和便利构造器)截图: 意思是:为初始化父类的init方法 意思是:必须...

  • 便利构造器

    类方法 调用方式 构造方法 便利构造函数(推荐使用)1. convenience开头2. 在构造函数中必须明确调用...

  • 便利构造器

    遍历构造器封装了对象创建的过程: 内部实现:封装了alloc和初始化操作,创建对象更加方便快捷. .h文件中: ....

  • Swift中重写`init( )`方法时无需调用`super.i

    根据Swift的构造器生成规则: 如果自定义指定构造器,必须在自定义构造方法里调用父类的指定构造器; 如果自定义便...

网友评论

  • 鬼谷门生:楼主您好 像大多的自定义alert是添加到window上的,这样当alert在显示时我们突然按home退至后台,当即将变为活跃时比如应用会模态出一个视图,比如手势解码,此时alert会显示在模态视图之上,这样不符合应用逻辑,应该怎么解决呢,试了好多方案没有解决,谢谢
  • 0x00chen:博主,这个怎么加一个蒙版
    轩辕小羽:@slowHand 在view后面加一个maskview 大小跟控制器的view一样 将这个view居中添加在maskview上
  • 明心见性:请问 swift 版本的实现了吗? swift 版本的 UIColor 2进制转 RGB 该怎么写
  • 0294b96fe4db:来个swift版的 急需
    0294b96fe4db:@swifter 多谢 多谢
    轩辕小羽:@swifter 好的,那我明天就写这个了
  • 与世倾听X游定终生:正好用到
    轩辕小羽:@与世倾听X游定终生 嗯嗯,能帮助到你就好!:smile:
  • 曾樑:热心肠好评

本文标题:iOS-自定义AlertView(便利构造器)

本文链接:https://www.haomeiwen.com/subject/oynshttx.html