Objective-C自定义弹层UIActionSheet
来源:互联网 发布:易语言 tcp 发送数据 编辑:程序博客网 时间:2024/06/07 14:23
系统弹层不好看?那就自己封装一个弹层小控件吧~
新建一个类,继承自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地址:
点击打开链接
- Objective-C自定义弹层UIActionSheet
- 自定义UIActionSheet
- 自定义UIActionSheet
- UIActionsheet 自定义
- 自定义UIActionSheet
- 自定义UIActionSheet
- 自定义UIActionSheet
- Objective-C-iOS常用小控件(UIActionSheet )动作列表
- objective-c 自定义类
- Objective-C 自定义UICollectionView
- iOS 自定义UIActionSheet
- 自定义Objective-c的ToastView
- Objective-C - 自定义构造方法
- Objective-C 自定义构造方法
- Objective-C tableView自定义cell
- Objective-C自定义UITabBar位置
- Objective-C中Sqlite3持久层框架
- IOS开发之自定义UIActionSheet
- CSS Selector(berserkjs用到)
- java 四舍五入保留小数的诡异
- 飞行姿态解算笔记(一)
- 飞行姿态解算笔记(二)
- 一般PID的C语言实现
- Objective-C自定义弹层UIActionSheet
- Block传值与协议传值的比较
- python mysql操作使用变量作为表名
- 笔记本电脑屏幕变暗的原因及解决方法
- 动态表单添加及动态删除元素
- 「试用」是比「面试」更有效的招聘策略
- Android卸载程序之后跳转到指定的反馈页面
- 制作一个有界面的OCX
- 互联网电商大数据一般架构