Objective-C自定义弹层UIActionSheet
2016-01-07 18:03
495 查看
系统弹层不好看?那就自己封装一个弹层小控件吧~
新建一个类,继承自UIView,(类名MessagePopView)
头文件中的代码:
#import <UIKit/UIKit.h>
@interface MessagePopView : UIView
-(void) hidePopView;
-(void) showPopView;
@end
.m文件实现:
#import "MessagePopView.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface MessagePopView ()
@property (strong, nonatomic)UIImageView *imgView;
@property (strong, nonatomic)UIImageView *imgViewMessage;
@property (strong, nonatomic)UILabel *backgroundLabel;
@property (strong, nonatomic)UIView *contentView;
@property (nonatomic, strong)UIButton *clearMessageBtn;
@property (nonatomic, strong)UIButton *cancelBtn;
@end
@implementation MessagePopView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self drawView];
}
return self;
}
- (void)drawView{
//半透明背景
self.imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"show_pop_view_bk"]];
self.imgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
self.imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePopView)];//添加手势,点击隐藏
[self.imgView addGestureRecognizer:tapGesture];
[self addSubview:self.imgView];
//弹层容器视图
self.contentView = [[UIView alloc]init];
self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100);
//弹层背景图
self.imgViewMessage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"show_manage_pop_view_bk"]];
self.imgViewMessage.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
self.imgViewMessage.userInteractionEnabled = YES;
[self.contentView addSubview:self.imgViewMessage];
self.clearMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.clearMessageBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 45);
self.clearMessageBtn.backgroundColor = [UIColor whiteColor];
[self.clearMessageBtn setTitle:@"确认" forState:UIControlStateNormal];
[self.clearMessageBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.clearMessageBtn addTarget:self action:@selector(MessageClick) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.clearMessageBtn];
self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.cancelBtn.frame = CGRectMake(0, 55, SCREEN_WIDTH, 45);
self.cancelBtn.backgroundColor = [UIColor whiteColor];
[self.cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[self.cancelBtn addTarget:self action:@selector(hidePopView) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.cancelBtn];
[self.imgView addSubview:self.contentView];
}
//点击确认的事件
- (void)MessageClick{
/**
* 这里添加其他操作
*/
[self hidePopView];
}
//隐藏弹层
-(void) hidePopView
{
[UIView animateWithDuration:0.3 animations:^{
self.contentView.frame = CGRectMake(0.0f, SCREEN_HEIGHT + 100 , self.frame.size.width, self.contentView.frame.size.height );
} completion:^(BOOL finished) {
self.hidden = YES;
[self removeFromSuperview];
}];
}
//显示弹层
-(void) showPopView
{
[UIView animateWithDuration:0.5 animations:^{
self.hidden = NO;
self.contentView.frame = CGRectMake(0.0f, SCREEN_HEIGHT - 100, self.frame.size.width, self.contentView.frame.size.height);
}];
self.imgView.hidden = NO;
}
@end
调用:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"点我" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
}
- (void)show{
_MessagePopView = [MessagePopView new];
[_MessagePopView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_MessagePopView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
[self.view addSubview:_MessagePopView];
[_MessagePopView showPopView];
}
效果图:
自己切不好图的话也可用UILabel代替~设置一下透明度就好
git地址:
点击打开链接
新建一个类,继承自UIView,(类名MessagePopView)
头文件中的代码:
#import <UIKit/UIKit.h>
@interface MessagePopView : UIView
-(void) hidePopView;
-(void) showPopView;
@end
.m文件实现:
#import "MessagePopView.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface MessagePopView ()
@property (strong, nonatomic)UIImageView *imgView;
@property (strong, nonatomic)UIImageView *imgViewMessage;
@property (strong, nonatomic)UILabel *backgroundLabel;
@property (strong, nonatomic)UIView *contentView;
@property (nonatomic, strong)UIButton *clearMessageBtn;
@property (nonatomic, strong)UIButton *cancelBtn;
@end
@implementation MessagePopView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self drawView];
}
return self;
}
- (void)drawView{
//半透明背景
self.imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"show_pop_view_bk"]];
self.imgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
self.imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePopView)];//添加手势,点击隐藏
[self.imgView addGestureRecognizer:tapGesture];
[self addSubview:self.imgView];
//弹层容器视图
self.contentView = [[UIView alloc]init];
self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100);
//弹层背景图
self.imgViewMessage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"show_manage_pop_view_bk"]];
self.imgViewMessage.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
self.imgViewMessage.userInteractionEnabled = YES;
[self.contentView addSubview:self.imgViewMessage];
self.clearMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.clearMessageBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 45);
self.clearMessageBtn.backgroundColor = [UIColor whiteColor];
[self.clearMessageBtn setTitle:@"确认" forState:UIControlStateNormal];
[self.clearMessageBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.clearMessageBtn addTarget:self action:@selector(MessageClick) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.clearMessageBtn];
self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.cancelBtn.frame = CGRectMake(0, 55, SCREEN_WIDTH, 45);
self.cancelBtn.backgroundColor = [UIColor whiteColor];
[self.cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[self.cancelBtn addTarget:self action:@selector(hidePopView) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.cancelBtn];
[self.imgView addSubview:self.contentView];
}
//点击确认的事件
- (void)MessageClick{
/**
* 这里添加其他操作
*/
[self hidePopView];
}
//隐藏弹层
-(void) hidePopView
{
[UIView animateWithDuration:0.3 animations:^{
self.contentView.frame = CGRectMake(0.0f, SCREEN_HEIGHT + 100 , self.frame.size.width, self.contentView.frame.size.height );
} completion:^(BOOL finished) {
self.hidden = YES;
[self removeFromSuperview];
}];
}
//显示弹层
-(void) showPopView
{
[UIView animateWithDuration:0.5 animations:^{
self.hidden = NO;
self.contentView.frame = CGRectMake(0.0f, SCREEN_HEIGHT - 100, self.frame.size.width, self.contentView.frame.size.height);
}];
self.imgView.hidden = NO;
}
@end
调用:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"点我" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
}
- (void)show{
_MessagePopView = [MessagePopView new];
[_MessagePopView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_MessagePopView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
[self.view addSubview:_MessagePopView];
[_MessagePopView showPopView];
}
效果图:
自己切不好图的话也可用UILabel代替~设置一下透明度就好
git地址:
点击打开链接
相关文章推荐
- java22:API-Object-String
- Objective-C构造方法
- Objective-C构造方法
- Objective-C特有类型——id
- Objective-C特有类型——id
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之六:拾遗
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之五:协议与分类
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之四:Method Swizzling
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之三:方法与消息
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之二:成员变量与属性
- com.alibaba.fastjson.JSONObject cannot be cast to
- Objective-C内存管理之引用计数
- oc之数组排序 id nsobject instancetype的区别
- NSObject 模态问题
- Objective-C 计算代码运行时间
- Objective-C 对 URL 进行 URLEncode 编码
- id和nstancetype、NSObject的区别
- id NSObject instanceType 区别
- object-c 子类覆盖父类属性 详解
- Objective-C点语法