#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

iOS- 自定义UIView (测试block和代理)的更多相关文章

  1. IOS自定义UIView

    IOS中一般会用到几种方式自定义UIView 1.继承之UIView的存代码的自定义View 2.使用xib和代码一起使用的自定义View 3.存xib的自定义View(不需要业务处理的那种) 本文主 ...

  2. OpenGL ES: iOS 自定义 UIView 响应屏幕旋转

    iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...

  3. iOS:UIView的block函数实现转场动画---双视图

    使用UIView动画函数实现转场动画——双视图 + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView durati ...

  4. iOS:UIView的block函数实现转场动画---单视图

    使用UIView动画函数实现转场动画——单视图 + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration ...

  5. iOs 自定义UIView 日历的实现 Swift2.1

    学习Swift有一个月了,动手写一个UIView吧. 所有源代码在最后,直接用就可以了,第一次写Swift,和C#,Java还是有区别的 (博客园可以考虑在代码插入中添加Swift的着色了) 1  函 ...

  6. iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)

     iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action)   今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...

  7. iOS block和代理的区别

      block和代理是iOS开发中实现回调的两种方式,大多数情况下是用哪个都可以,主要看个人喜好.本文主要是对两者做一下对比. 1.block简介   在 iOS中, block一共分三种.   (1 ...

  8. IOS xib和代码自定义UIView

    https://www.jianshu.com/p/1bcc29653085 总结的比较好 iOS开发中,我们常常将一块View封装起来,以便于统一管理内部的子控件. 下面就来说说自定义View的封装 ...

  9. 荼菜的iOS笔记--UIView的几个Block动画

    前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...

随机推荐

  1. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  2. tomcat的host配置

    本机 etc\hosts 首先了解C:\WINDOWS\system32\drivers\etc\hosts文件配置 127.0.0.1 static1.ezsins.com #adoble ps c ...

  3. B. Rebranding

    http://codeforces.com/problemset/problem/591/B   B. Rebranding time limit per test 2 seconds memory ...

  4. oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)

    数据库返回的是结果集,存储过程返回的是一个或者多个值,所以不要使用while循环去读取,也不要使用datareader函数去调用.v_class_name是返回函数 使用.net调用oracle数据库 ...

  5. 30几个HTML5经典动画应用回顾 让你大饱眼福

    周末大放送,让我们来回顾一下HTML5经典动画应用,一定会让你大饱眼福. 1.HTML5 Canvas画板画图工具 可定义笔刷和画布 HTML5 Canvas还有一个比较实用的应用,那就是网络画板,这 ...

  6. Fragment实现底部选项卡切换效果

    现在很多APP的样式都是底部选项卡做为首页的,实现这样的效果,我们一般有这样几种方式,第一,最屌丝的做法,我直接自定义选项卡视图,通过监听选项卡视图,逻辑控制内容页的切换,这样做的想法一般是反正这几个 ...

  7. Firefox 设置技巧

    在Firefox地址栏中输入“about:cache”并键入回车,接着将显示Firefox的内存缓冲设置与磁盘高速缓存设置.如果在页面上单击“List Cache Entries”链接,我们还可以查看 ...

  8. 负载均衡SESSION同步总结

    1.redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题 http://www.cnblogs.com/painsOnline/p/5194851.h ...

  9. Apache实现动态虚拟主机

    经常在开发中为Apache web server添加虚拟主机  方便多个项目的 同时运营,但是每次增加新的项目时都得重新配置增加VirtualHost:虚拟主机    部分,时间久了VirtualHo ...

  10. HTML第三天学习笔记

    昨天学的超链接,今天深入学习了下,发现了更多的知识点,而且关于初始新建网页时,由于是初学者,所以还是纯手写代码~ <html> <head> <title>超链接& ...