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

iOS- 自定义UIView (测试block和代理)的更多相关文章
- IOS自定义UIView
IOS中一般会用到几种方式自定义UIView 1.继承之UIView的存代码的自定义View 2.使用xib和代码一起使用的自定义View 3.存xib的自定义View(不需要业务处理的那种) 本文主 ...
- OpenGL ES: iOS 自定义 UIView 响应屏幕旋转
iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...
- iOS:UIView的block函数实现转场动画---双视图
使用UIView动画函数实现转场动画——双视图 + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView durati ...
- iOS:UIView的block函数实现转场动画---单视图
使用UIView动画函数实现转场动画——单视图 + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration ...
- iOs 自定义UIView 日历的实现 Swift2.1
学习Swift有一个月了,动手写一个UIView吧. 所有源代码在最后,直接用就可以了,第一次写Swift,和C#,Java还是有区别的 (博客园可以考虑在代码插入中添加Swift的着色了) 1 函 ...
- iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)
iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action) 今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...
- iOS block和代理的区别
block和代理是iOS开发中实现回调的两种方式,大多数情况下是用哪个都可以,主要看个人喜好.本文主要是对两者做一下对比. 1.block简介 在 iOS中, block一共分三种. (1 ...
- IOS xib和代码自定义UIView
https://www.jianshu.com/p/1bcc29653085 总结的比较好 iOS开发中,我们常常将一块View封装起来,以便于统一管理内部的子控件. 下面就来说说自定义View的封装 ...
- 荼菜的iOS笔记--UIView的几个Block动画
前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...
随机推荐
- 1.VS2010C++环境设置
一.需要下载的软件 1.visual studio 2010\\xxzx\tools\编程工具\MICROSOFT\VISUAL.STUDIO\VISUAL.STUDIO.201032位cn_visu ...
- qq邮箱发送
454 Authentication failed, please open smtp flag first!用QQ邮箱测试报错 我用QQ邮箱测试javamail发送邮件的功能,用户名密码设置正确,却 ...
- 使用logmnr方法找回被误删除Oracle的数据的脚本
俗话说,常在河边走,哪有不湿鞋的.作为一个经常与数据库打交道的程序员,偶尔不小心误删除或误操作的数据也是在所难免的.如果是Oracle数据库,这里给您介绍一种从日志中找回数据的办法,下面这个地址是我以 ...
- HDU1151Air Raid(二分图的最大匹配)
题目大意: 有一个城镇,它的所有街道都是单行的,并且每条街道都是和两个路口相连.同时已知街道不会形成回路. 你的任务是编写程序求最小数量的伞兵,这些伞兵可以访问(visit)所有的路口.
- iOS的几种定时器
//gcd的定时器timer必须先保存为一个属性或者成员变量 @property (nonatomic , assign) dispatch_source_t timer; //第一种 每一秒执行一次 ...
- UITextfiled子视图被剪切
一般情况下,如果在view1上面添加了view2,但是view2超出了view1,也会在屏幕上面显示超出的部分 例如: UIButton *button =[[UIButton alloc]initW ...
- 消息系统Kafka介绍 - 董的博客
1. 概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...
- 字符串左移n位操作
void reverse(char* str, int begin, int end) { char temp; for( ; begin < end; begin++) { temp = st ...
- (剑指Offer)面试题24:二叉搜索树的后序遍历序列
题目: 输入一个整数数组,判断该数组是不是某个二叉搜索树的后序遍历的结果,如果是则返回true,否则返回false. 假设输入的数组的任意两个数字都互不相同. 思路: 根据二叉搜索树的后序遍历特点,很 ...
- VC++内存区域
转载声明:本文转载自http://blog.csdn.net/sjxbf/article/details/6441730 一.预备知识—程序的内存分配 一个由c/c++编译的程序占用的内存分为以下几个 ...