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地址:

点击打开链接

1 0
原创粉丝点击