先看效果

做法
新建一个类,继承自UIView。
在.h文件中,
@interface PDmidTipView : UIView
@property(nonatomic,strong)NSString *text;
-(void)show; // 显示
@end
在.m 文件中
子控件的声明
@interface PDmidTipView()
@property(nonatomic,weak)UILabel *titleLb; // 标题
@property(nonatomic,weak)UILabel *textLb; // 具体内容
@property(nonatomic,weak)UIButton *finshBtn; // 确定按钮
@property(nonatomic,strong)UIView *cover; // 蒙版view
@end
懒加载cover
-(UIView *)cover{
if(!_cover){
_cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
_cover.backgroundColor = LineColor;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCoverView)];
[_cover addGestureRecognizer:tap]; //给蒙版添加手势
}
return _cover;
}
创建方法
-(instancetype)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
self.backgroundColor = [UIColor whiteColor];
UILabel *lb0 = [[UILabel alloc] Label_color3:Font18 text:@"推荐理由"];
[self addSubview:lb0];
_titleLb = lb0;
UILabel *lb = [[UILabel alloc] Label_color8:Font16 text:nil];
lb.numberOfLines = 0;
[self addSubview:lb];
_textLb = lb;
UIButton *btn = [[UIButton alloc] buttonOfRed:@"完成" font:Font18];
[btn setBackgroundColor:Color_red];
[btn.layer setCornerRadius:0.0];
[self addSubview:btn];
_finshBtn = btn;
[_finshBtn addTarget:self action:@selector(clickFinish) forControlEvents:UIControlEventTouchUpInside];
float h = screenH == 812?65:45;
_finshBtn.frame = CGRectMake(0, self.frame.size.height - h, screenW, h);
[_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.top.equalTo(self.mas_top).offset(20);
make.height.equalTo(@18);
}];
[_textLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_titleLb.mas_bottom).offset(40);
make.left.equalTo(self.mas_left).offset(20);
make.right.equalTo(self.mas_right).offset(-20);
}];
}
return self;
}
事件处理
//--点击完成按钮
-(void)clickFinish{
[self dismiss]; //消失
}
//--点击蒙版
-(void)clickCoverView{
[self dismiss]; //消失
}
显示与消失的实现(动画效果)
-(void)show{
self.center = CGPointMake(screenW/2 , screenH*1.5);
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self.cover];
[window addSubview:self];
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(screenW/2 , screenH*0.65);
} completion:^(BOOL finished) {
//NSLog(@"所有动画之行完毕");
}];
}
- (void)dismiss {
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(screenW/2 , screenH*1.5);
} completion:^(BOOL finished) {
[self removeFromSuperview];
[_cover removeFromSuperview];
}];
}
网友评论