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就会 ...
随机推荐
- [翻译][Trident] Trident state原理
原文地址:https://github.com/nathanmarz/storm/wiki/Trident-state ----------------------------- Trident在读写 ...
- C# JSON使用的常用技巧(一)
获取JSON无格式的字符串: new JArray().ToString(Newtonsoft.Json.Formatting.None): 如: {"A":123,"B ...
- PL/SQL devloper 常用设置
1)代码自动完成 Tools->Preferences->User Interface->Key Configuration. 找到Tools/Code Assistant,修改为自 ...
- 选择一本C++教材
从上周开始写如何使用C++编程以后,我发现这不是一个容易的题目.因此,我认真的看了一下C++相关的材料,发现现在为止,比较好的材料还是这些: 初学者: Accelerated C++,这是一本学习起来 ...
- c++中的signal机制
简介 signal是为了解决类之间通信的问题而出现的,更深入的原因是面向对象讲究封装,但是封装必然导致类之间沟通困难,但是使用接口的方式又太重量级--需要写很多代码,而且会导致接口爆炸 比如你需要把一 ...
- Unity3d:使用uWebKit插件嵌入网页,网页中的flv视频无法播放
问题描述:unity3d程序,使用uWebKit插件嵌入网页,用来播放FLV视频,有的电脑可以正常播放,有的电脑在网页中播放不了ps:网页中的播放器用的是player.swf解决方案:是由于网页中的播 ...
- Javascript 装载和执行(copy的感觉有很多错误。。)
copy from:http://coolshell.cn/articles/9749.html 首先,我想说一下Javascript的装载和执行.通常来说,浏览器对于Javascript的运行有两大 ...
- jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏
1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...
- [Angular2 Router] Use Params from Angular 2 Routes Inside of Components
Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. ...
- spring mvc 3.0 ModelAndView模型视图类
见名知意,从名字上我们可以知道ModelAndView中的Model代表模型,View代表视图.即,这个类把要显示的数据存储到了Model属性中,要跳转的视图信息存储到了view属性.我们看一下Mod ...