• iOS- 自定义UIView (测试block和代理)


    #import <UIKit/UIKit.h>

    typedef void(^compeletionHandler) (NSInteger selectButtonIndex);

    @class ZSDCustom;

    @protocol ZSDCustomDelegate <NSObject>

    -(void)showCustomView:(ZSDCustom *)customView andButtonClick:(NSInteger)selectIndex;

    @end

    @interface ZSDCustom : UIView

    @property(nonatomic,copy)compeletionHandler myHandler;

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

    -(void)showCustomView:(ZSDCustom *)customView andDelegate:(id)delegate andCompeleteHandler:(compeletionHandler)handler;

    @end

    #import "ZSDCustom.h"

    #define kDialogWidth 280.0f

    #define kButtonSize CGSizeMake(115.0f,45.0f)

    @implementation ZSDCustom

    {

        UIView *dialogView;

    }

    -(id)initWithFrame:(CGRect)frame

    {

        if (self=[super initWithFrame:frame])

        {

            [self setUp];

        }

        return self;

    }

    -(void)setUp

    {

        self.frame = [UIScreen mainScreen].bounds;

        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];

        

        CGRect frect = CGRectZero;

        frect.size.width = kDialogWidth;

        dialogView = [[UIView alloc]initWithFrame:frect];

        dialogView.backgroundColor = [UIColor whiteColor];

        dialogView.layer.cornerRadius = 4.0f;

        dialogView.layer.masksToBounds = YES;

        [self addSubview:dialogView];

        

        //按钮

        frect.origin.x = 15.0f;

        frect.origin.y = 56.0f + 20.0f;

        frect.size = kButtonSize;

        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        cancelBtn.frame = frect;

        [cancelBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];

        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];

        [cancelBtn setBackgroundColor:[UIColor colorWithRed:243.0 / 255.0f green:243.0 / 255.0f blue:243.0 / 255.0f alpha:1.0]];

        [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [cancelBtn.layer setBorderWidth:1.0];

        [cancelBtn.layer setBorderColor:[UIColor grayColor].CGColor];

        [cancelBtn.layer setCornerRadius:4.0f];

        [cancelBtn setTag:1];

        [dialogView addSubview:cancelBtn];

        

        frect.origin.x = kDialogWidth - 15.0f - kButtonSize.width;

        UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        okBtn.frame = frect;

        [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [okBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];

        [okBtn setTitle:@"确认" forState:UIControlStateNormal];

        [okBtn setBackgroundColor:[UIColor colorWithRed:207.0 / 255.0f green:44.0 / 255.0f blue:65.0 / 255.0f alpha:1.0]];

        [okBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [okBtn.layer setCornerRadius:4.0f];

        [okBtn setTag:2];

        [dialogView addSubview:okBtn];

        

        float totalHeight = CGRectGetMaxY(okBtn.frame) + 20.0f;

        frect = dialogView.frame;

        frect.origin.x = (self.bounds.size.width - kDialogWidth) / 2.0f;

        frect.origin.y = (self.bounds.size.height - totalHeight) / 2.0f;

        frect.size.height = totalHeight;

        dialogView.frame = frect;

    }

    -(void)buttonClick:(UIButton *)sender

    {

        if (_myHandler) {

            _myHandler(sender.tag);

        }

        if (sender.tag==2) {

            if (_delegate&&[_delegate respondsToSelector:@selector(showCustomView:andButtonClick:)]) {

                

               [_delegate showCustomView:self andButtonClick:sender.tag];

            }

        }

        else

        {

            [self hide];

        }

    }

    -(void)show

    {

        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

        [keyWindow addSubview:self];

        

        dialogView.alpha = 0;

        dialogView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);

        

        [UIView animateWithDuration:0.3f animations:^{

            dialogView.alpha = 1.0;

            dialogView.transform = CGAffineTransformMakeScale(1.0, 1.0);

        }];

    }

    -(void)hide

    {

        [UIView animateWithDuration:0.3f animations:^{

            dialogView.alpha = 0;

            dialogView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);

        } completion:^(BOOL finished) {

            [self removeFromSuperview];

        }];

    }

    -(void)showCustomView:(ZSDCustom *)customView andDelegate:(id)delegate andCompeleteHandler:(compeletionHandler)handler

    {

        [customView show];

        _delegate=delegate;

        _myHandler=handler;

    }

    @end

    #import "ViewController.h"

    #import "ZSDCustom.h"

    @interface ViewController ()<ZSDCustomDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    - (IBAction)showViewBtnClick:(UIButton *)sender

    {

        

        ZSDCustom *custom=[[ZSDCustom alloc]initWithFrame:CGRectZero];

        

        [custom showCustomView:custom andDelegate:self andCompeleteHandler:^(NSInteger selectButtonIndex) {

            

            NSLog(@"selectButtonIndex=%ld",selectButtonIndex);

        }];

    }

    -(void)showCustomView:(ZSDCustom *)customView andButtonClick:(NSInteger)selectIndex

    {

     NSLog(@"selectIndex=%ld",selectIndex);

    }

    @end

  • 相关阅读:
    过度使用DBLINK做系统集成会带来的问题
    微服务架构优缺点
    linux + svn提交日志不能显示 日期一直都是1970-01-01
    maven 无法导入ojdbc 的jar包 解决方法
    认识webservice
    tensorflow-gpu2.1缺少libcudnn.so.7
    tensorflow-gpu2.1.0报错 so returning NUMA node zero解决办法
    基于YOLO-V2的行人检测(自训练)附pytorch安装方法
    电脑键盘背景灯无法控制
    pip升级失败
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4122546.html
Copyright © 2020-2023  润新知