自定义View弹出,周围变暗,点击view以外的区域,view消失及变亮。
2015-12-16 17:09
387 查看
AlertView.h
#import <UIKit/UIKit.h>
@protocol AlertViewDelegate <NSObject>
- (void)alertViewButtonClick ;
@end
@interface AlertVIew :
UIView
- (void)diss;//
视图消失
@property (nonatomic,
assign) id<AlertViewDelegate>delegate;
@end
AlertView.m
#import "AlertVIew.h"
#define KLeftWitdh 10
@interface AlertVIew ()
@property (nonatomic,
strong) UITapGestureRecognizer * tapGesture;
@end
@implementation AlertVIew
-(instancetype)initWithFrame:(CGRect)frame {
self = [super
initWithFrame:frame];
if (self) {
UIWindow * window = [UIApplication
sharedApplication].delegate.window;
//
如果手势没有值
if (!self.tapGesture) {
self.tapGesture = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tapGesture:)];
}
//
如果不包含这个手势
if (![window.gestureRecognizers
containsObject:self.tapGesture]) {
[window addGestureRecognizer:self.tapGesture];
}
UITextField * textFiled = [[UITextField
alloc] initWithFrame:CGRectMake(KLeftWitdh
,
KLeftWitdh
,
CGRectGetWidth(self.frame) -
KLeftWitdh * 2
, (CGRectGetHeight(self.frame) -
KLeftWitdh *4) /
3)];
textFiled.backgroundColor = [UIColor
redColor];
[self addSubview:textFiled];
UITextField * textField2 = [[UITextField
alloc] initWithFrame:CGRectMake(KLeftWitdh
,
KLeftWitdh * 2 +
CGRectGetHeight(textFiled.frame)
,
CGRectGetWidth(textFiled.frame)
,
CGRectGetHeight(textFiled.frame))];
textField2.backgroundColor = [UIColor
redColor];
[self addSubview:textField2];
UIButton * button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.frame =
CGRectMake(KLeftWitdh
, KLeftWitdh *
3 + CGRectGetHeight(textFiled.frame) *
2
, CGRectGetWidth(self.frame) -
2 * KLeftWitdh
, CGRectGetHeight(textFiled.frame));
[button addTarget:self
action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"保存"
forState:UIControlStateNormal];
button.backgroundColor = [UIColor
blueColor];
[self addSubview:button];
}
return
self;
}
- (void)tapGesture:(UIGestureRecognizer *)gesture {
NSLog(@"点击了");
if (gesture.state ==
UIGestureRecognizerStateEnded) {
//
获取触摸点在window上的坐标
CGPoint touchPoint = [gesture
locationInView:nil];
//
转换成以alertView为基准的坐标
CGPoint converPoint = [self
convertPoint:touchPoint
fromView:self];
//
判断是否点在alertView之外
if (!CGRectContainsPoint(self.frame, converPoint)) {
NSLog(@"不点在AlertView里面");
if (_delegate) {
[_delegate
alertViewButtonClick];
[self
diss];
UIWindow * window = [UIApplication
sharedApplication].delegate.window;
[window removeGestureRecognizer:self.tapGesture];
return;
}
}
NSLog(@"点在外面");
}
cdd3
}
// 移除视图
- (void)diss {
[self
removeFromSuperview];
}
// 保存按钮
- (void)buttonAction {
if (_delegate) {
[_delegate
alertViewButtonClick];
[self diss];
}
}
@end
#import "ViewController.h"
#import "AlertVIew.h"
#define MainScreenWidth [UIScreen mainScreen].bounds.size.width
#define MainScreenHitght [UIScreen mainScreen].bounds.size.height
@interface
ViewController ()<AlertViewDelegate>
@property (nonatomic,
strong) AlertVIew * alertView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
UIButton * button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.center =
CGPointMake(CGRectGetWidth(self.view.frame)
/ 2,
CGRectGetHeight(self.view.frame)
/ 2);
button.bounds =
CGRectMake(0,
0, 100, 100);
[button addTarget:self
action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor
redColor];
[self.view
addSubview:button];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view
endEditing:YES];
}
// 点击button和view以外区域的回调方法
- (void)alertViewButtonClick {
self.view.alpha
= 1;//
改变透明度模拟变亮
}
- (void)buttonAction {
_alertView = [[AlertVIew
alloc]
initWithFrame:CGRectMake((MainScreenWidth -
MainScreenWidth / 1.5) /
2
, (MainScreenHitght -
MainScreenWidth /
1.5 / 3.0) /
2
,
MainScreenWidth / 1.5
,
MainScreenWidth / 1.5 /
3.0)];
_alertView.backgroundColor = [UIColor
yellowColor];
self.view.alpha
= 0.5;//
改变透明度模拟变暗
_alertView.delegate =
self;
[self.view.window
addSubview:_alertView];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@protocol AlertViewDelegate <NSObject>
- (void)alertViewButtonClick ;
@end
@interface AlertVIew :
UIView
- (void)diss;//
视图消失
@property (nonatomic,
assign) id<AlertViewDelegate>delegate;
@end
AlertView.m
#import "AlertVIew.h"
#define KLeftWitdh 10
@interface AlertVIew ()
@property (nonatomic,
strong) UITapGestureRecognizer * tapGesture;
@end
@implementation AlertVIew
-(instancetype)initWithFrame:(CGRect)frame {
self = [super
initWithFrame:frame];
if (self) {
UIWindow * window = [UIApplication
sharedApplication].delegate.window;
//
如果手势没有值
if (!self.tapGesture) {
self.tapGesture = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tapGesture:)];
}
//
如果不包含这个手势
if (![window.gestureRecognizers
containsObject:self.tapGesture]) {
[window addGestureRecognizer:self.tapGesture];
}
UITextField * textFiled = [[UITextField
alloc] initWithFrame:CGRectMake(KLeftWitdh
,
KLeftWitdh
,
CGRectGetWidth(self.frame) -
KLeftWitdh * 2
, (CGRectGetHeight(self.frame) -
KLeftWitdh *4) /
3)];
textFiled.backgroundColor = [UIColor
redColor];
[self addSubview:textFiled];
UITextField * textField2 = [[UITextField
alloc] initWithFrame:CGRectMake(KLeftWitdh
,
KLeftWitdh * 2 +
CGRectGetHeight(textFiled.frame)
,
CGRectGetWidth(textFiled.frame)
,
CGRectGetHeight(textFiled.frame))];
textField2.backgroundColor = [UIColor
redColor];
[self addSubview:textField2];
UIButton * button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.frame =
CGRectMake(KLeftWitdh
, KLeftWitdh *
3 + CGRectGetHeight(textFiled.frame) *
2
, CGRectGetWidth(self.frame) -
2 * KLeftWitdh
, CGRectGetHeight(textFiled.frame));
[button addTarget:self
action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"保存"
forState:UIControlStateNormal];
button.backgroundColor = [UIColor
blueColor];
[self addSubview:button];
}
return
self;
}
- (void)tapGesture:(UIGestureRecognizer *)gesture {
NSLog(@"点击了");
if (gesture.state ==
UIGestureRecognizerStateEnded) {
//
获取触摸点在window上的坐标
CGPoint touchPoint = [gesture
locationInView:nil];
//
转换成以alertView为基准的坐标
CGPoint converPoint = [self
convertPoint:touchPoint
fromView:self];
//
判断是否点在alertView之外
if (!CGRectContainsPoint(self.frame, converPoint)) {
NSLog(@"不点在AlertView里面");
if (_delegate) {
[_delegate
alertViewButtonClick];
[self
diss];
UIWindow * window = [UIApplication
sharedApplication].delegate.window;
[window removeGestureRecognizer:self.tapGesture];
return;
}
}
NSLog(@"点在外面");
}
cdd3
}
// 移除视图
- (void)diss {
[self
removeFromSuperview];
}
// 保存按钮
- (void)buttonAction {
if (_delegate) {
[_delegate
alertViewButtonClick];
[self diss];
}
}
@end
#import "ViewController.h"
#import "AlertVIew.h"
#define MainScreenWidth [UIScreen mainScreen].bounds.size.width
#define MainScreenHitght [UIScreen mainScreen].bounds.size.height
@interface
ViewController ()<AlertViewDelegate>
@property (nonatomic,
strong) AlertVIew * alertView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
UIButton * button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.center =
CGPointMake(CGRectGetWidth(self.view.frame)
/ 2,
CGRectGetHeight(self.view.frame)
/ 2);
button.bounds =
CGRectMake(0,
0, 100, 100);
[button addTarget:self
action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor
redColor];
[self.view
addSubview:button];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view
endEditing:YES];
}
// 点击button和view以外区域的回调方法
- (void)alertViewButtonClick {
self.view.alpha
= 1;//
改变透明度模拟变亮
}
- (void)buttonAction {
_alertView = [[AlertVIew
alloc]
initWithFrame:CGRectMake((MainScreenWidth -
MainScreenWidth / 1.5) /
2
, (MainScreenHitght -
MainScreenWidth /
1.5 / 3.0) /
2
,
MainScreenWidth / 1.5
,
MainScreenWidth / 1.5 /
3.0)];
_alertView.backgroundColor = [UIColor
yellowColor];
self.view.alpha
= 0.5;//
改变透明度模拟变暗
_alertView.delegate =
self;
[self.view.window
addSubview:_alertView];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- Jsp使用HttpSessionBindingListener实现在线人数记录
- 最少换乘
- iOS每日一记之------常用小工具代码合集
- Android之ListView实现图文信息混排(服务器端)(一)
- OpenStack 对象存储 Swift 简单介绍
- 【 bzoj 2286 】 : [Sdoi2011]消耗战 - 树形DP
- 希赛网 > 问答 > 程序开发 > Web开发 > Web前端 > Firefox下载文件中文乱码的一种Server端解决办法 Firefox下载文件中文乱码的一种Server端解决办法
- Linux负载均衡环境搭建-2 安装J2EE环境
- 线上Slave报1062的案例
- 最少换乘
- WAF防火墙
- ArcGis 10.1发布地图服务与访问
- 扩展RBAC用户角色权限设计方案Role-Based Access Control
- springMVC两种方式实现多文件上传及效率比较
- Spring 框架的设计理念与设计模式分析
- 关于TCP的三次握手建立连接
- 字符集SQL注入
- Objective--C UIScrollView 通讯录
- Undefined symbols for architecture x86_64 错误处理
- java基础(多线程)