ios首页投稿(暂停使用,暂停投稿)程序员

iOS-Study自定义视图view封装弹框AlterView

2016-07-31  本文已影响613人  麦穗0615

前言:我们开发的时候经常会自定义一些view,以备后用,今天从简书上看到一篇用view封装弹框的文章,就拿来学习学习。封装一下,以备学习,感谢那位作者。

效果图:


自定义AlterView.gif

目录:
1.简单的思路
2.代码示例
3.使用方法
4.学习要点
5.思路总结
6.学习体会
7.Demo地址以及原作者地址

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

  /**取消按钮点击事件*/
  typedef void (^cancelBlock)();
  /**确定按钮点击事件*/
  typedef void (^sureBlock)();
  @interface ZYAlterView : UIView
  /**失败回调*/
  @property(copy,nonatomic) cancelBlock cancel_block;
  /**成功回调*/
  @property(copy,nonatomic) sureBlock sure_block;
  /**
    *
    *  @param title           标题
    *  @param content         内容
    *  @param cancel          取消按钮内容
    *  @param sure            确定按钮内容
    *  @param cancelBlock   取消按钮点击事件
    *  @param sureBlock     确定按钮点击事件
    *
    *  @return ZYAlterView
    */
   +(instancetype)alterViewWithTitle:(NSString *)title
                             content:(NSString *)content
                              cancel:(NSString *)cancel
                                sure:(NSString *)sure
                  cancel_Block_Clcik:(cancelBlock)cancelBlock
                    sure_Block_Click:(sureBlock)sureBlock;
    @end

解析:

ZYAlterView.m
#import "ZYAlterView.h"

   #define KSCREEN_W  [UIScreen mainScreen].bounds.size.width
   #define KSCREEN_H   [UIScreen mainScreen].bounds.size.height
   @interface ZYAlterView ()
    /**标题lable*/
    @property(strong,nonatomic)UILabel *titleLab;
    /**内容lable*/
    @property(strong,nonatomic)UILabel *contentLab;
    /**取消Btn*/
    @property(strong,nonatomic)UIButton *cancelBtn;
    /**确定Btn*/
    @property(strong,nonatomic)UIButton *sureBtn;
    /**标题*/
    @property(copy,nonatomic)NSString *title;
    /**内容*/
    @property(copy,nonatomic)NSString *content;
    /**取消*/
    @property(copy,nonatomic)NSString *cancelTitle;
    /**确定*/
    @property(copy,nonatomic)NSString *sureTitle;
    @end
    @implementation ZYAlterView
    #pragma mark - 初始化(添加控件)
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self =  [super initWithFrame:frame];
        if (self) {
    >
    //   标题
    _titleLab = [[UILabel alloc]initWithFrame:(CGRect){0,0,self.bounds.size.width,50}];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.textColor = [UIColor blackColor];
    [self addSubview:_titleLab];
    >
    //   内容
    _contentLab = [[UILabel alloc]initWithFrame:(CGRect){0,CGRectGetMaxY(_titleLab.frame),self.bounds.size.width,50}];
    _contentLab.textAlignment = NSTextAlignmentCenter;
    _contentLab.textColor = [UIColor redColor];
    [self addSubview:_contentLab];
    >
    //   取消按钮
    _cancelBtn = [[UIButton alloc]initWithFrame:(CGRect){0,CGRectGetMaxY(_contentLab.frame),self.bounds.size.width/2,50}];
    _cancelBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _cancelBtn.layer.borderWidth = 0.5;
    [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelBtClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancelBtn];
    >
    //   确定按钮
    _sureBtn = [[UIButton alloc]initWithFrame:(CGRect){self.bounds.size.width/2,CGRectGetMaxY(_contentLab.frame),self.bounds.size.width/2,50}];
    _sureBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _sureBtn.layer.borderWidth = 0.5;
    [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sureBtn addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sureBtn];
   }
     return self;
   }
  #pragma mark----实现类方法
  +(instancetype)alterViewWithTitle:(NSString *)title
                                      content:(NSString *)content
                                       cancel:(NSString *)cancel
                                          sure:(NSString *)sure
                  cancel_Block_Clcik:(cancelBlock)cancelBlock
                      sure_Block_Click:(sureBlock)sureBlock
    {
      ZYAlterView *alterView = [[ZYAlterView alloc]initWithFrame:(CGRect){0,0,250,150}];
      alterView.backgroundColor = [UIColor whiteColor];
      alterView.center = (CGPoint){KSCREEN_W/2,KSCREEN_H/2};
      alterView.layer.cornerRadius = 5;
      alterView.layer.masksToBounds = YES;
      alterView.title=title;
      alterView.content=content;
      alterView.cancelTitle=cancel;
      alterView.sureTitle=sure;
      alterView.cancel_block=cancelBlock;
      alterView.sure_block=sureBlock;
      return alterView;
      }
    #pragma mark--给属性重新赋值
    -(void)setTitle:(NSString *)title
    {
        _titleLab.text  = title;
    }
    -(void)setContent:(NSString *)content
   {
      _contentLab.text = content;
   }
    -(void)setSureTitle:(NSString *)sureTitle
  {
     [_sureBtn setTitle:sureTitle forState:UIControlStateNormal];
  }
  - (void)setCancelTitle:(NSString *)cancelTitle
  {
    [_cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
  }
  #pragma mark----取消按钮点击事件
  -(void)cancelBtClick
  {
      [self removeFromSuperview];
      self.cancel_block();
  }
  #pragma mark----确定按钮点击事件
  -(void)sureBtClick
  {
      [self removeFromSuperview];
      self.sure_block();
     }
  @end

解析:
通过init初始化控件,在通过自定义的类方法,创建自定义的类,对其属性进行赋值,但是,此时,控件没有值。其实,为其属性赋值,就是写其属性的set方法,通过set方法,给本类上的控件赋值。如上代码。

在ViewController.m中 导入"#import "ZYAlterView.h""头文件调用代码:
ZYAlterView *alterView = [ZYAlterView alterViewWithTitle:@"自定义弹框学习" content:@"虚心学习,真爱生命!" cancel:@"取消" sure:@"确定" cancel_Block_Clcik:^{

    //取消按钮点击事件
  NSLog(@"取消");
   } sure_Block_Click:^{
    //确定按钮点击事件
    NSLog(@"确定");
   }];

ZYAlterView直接封装成了类方法,并且可以手动填写标题,内容,取消按钮的内容,确定按钮的内容,还有一个需要注意的就是笔者将取消按钮和确定按钮的点击事件利用block传递出来了,这点值得我们学习。

因为笔者将该view封装成了类方法,所以会调用这个类时会先执行
+(instancetype)alterViewWithTitle:(NSString *)title...
方法,紧接着执行
-(instancetype)initWithFrame:(CGRect)frame
方法,然后会继续执行alterView.title=title;属性赋值的方法,但是这时界面展示不出来内容的,需要在set方法中重新给相关内容赋值才会展示出来,最后俩个函数就是利用block将点击事件传递到外部类中。

上一篇下一篇

猜你喜欢

热点阅读