#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. ArrayList中元素去重问题

    如题所示,如果一个ArrayList中包含多个重复元素,该如何去重呢? 思路一以及实现: 声明2个ArrayList,分别为listA与listB ,listA为待去重list ,listB 保存去重 ...

  2. codeforces 659A Round House

    A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Javascript中的数据类型之旅

    虽然Javascript是弱类型语言,但是,它也有自己的几种数据类型,分别是:Number.String.Boolean.Object.Udefined.Null.其中,Object属于复杂数据类型, ...

  4. 显示MYSQL数据库信息

    显示所有的数据库:show databases 显示一个数据库所有表用:show tables from DatabaseName SELECT table_name FROM information ...

  5. c++中__declspec用法总结

    “__declspec”是Microsoft c++中专用的关键字,它配合着一些属性可以对标准C++进行扩充.这些属性有:align.allocate.deprecated. dllexport.dl ...

  6. Server-U_详细配置

    1.首先绿化 Server-U,运行 2.打开Server-U自动弹出如下图:如果不自动弹出,那点击界面上的 新建域  ------ 先有域再有用户,用户在域里面 4. 输入“名称”和“说明”,其中“ ...

  7. Expression<Func<T,TResult>>和Func<T,TResult> 与AOP与WCF

    1>>Expression<Func<T,TResult>>和Func<T,TResult>http://www.cnblogs.com/xcsn/p/ ...

  8. 毕业设计--天气预报App

    9月中旬,开始动手做我的毕业设计了,之前一直在纠结做啥,后来想想,既然是做毕业设计,那就大胆地做点自己没接触过的东西吧.然后网上查找资料得知做天气预报需要用到开放的API,而且要用那种现在还在维护的, ...

  9. ActionBarCompat

    布局文件中添加menu menu_main.xml <?xml version="1.0" encoding="utf-8"?> <menu ...

  10. C++的优秀特性3:构造函数和析构函数

    (转载请注明原创于潘多拉盒子) 构造函数和析构函数是C++中再熟悉不过的概念了,几乎每个了解一点C++的人都知道这两个概念是什么意思.一个对象的全部生命期中构造函数和析构函数执行的时机如下: 1. 为 ...