看了很多网上别人写的弹窗,总觉得比较复杂的样子,这不,就自己写了一个简单弹窗,请大家多多指教。
先附上代码<a href ="https://github.com/smileKH/HBPopView">HBPopView</a>
截图效果.h文件就一个方法
#import <UIKit/UIKit.h>
@interface HBPopView : UIView
- (void)show;
@end
.m文件
#import "HBPopView.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
@interface HBPopView()
//主要的Windows
@property (nonatomic, strong) UIWindow *backWindow;
//黑色的view
@property (nonatomic , strong)UIView * drakView;
//白色的view
@property (nonatomic, strong) UIView *bottomView;
//取消button
@property (nonatomic , strong)UIButton * cancelButton;
@end
实现代码如下
@implementation HBPopView
- (instancetype)init
{
self = [super init];
if (self) {
// 暗黑色的view
UIView *darkView = [[UIView alloc] init];
[darkView setAlpha:0];
[darkView setUserInteractionEnabled:NO];
[darkView setFrame:(CGRect){0, 0, SCREEN_WIDTH,SCREEN_HEIGHT}];
[darkView setBackgroundColor:[UIColor grayColor]];
[self addSubview:darkView];
self.drakView = darkView;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
[darkView addGestureRecognizer:tap];
// 所有按钮的底部view
UIView *bottomView = [[UIView alloc] init];
[bottomView setBackgroundColor:[UIColor whiteColor]];
[self addSubview:bottomView];
_bottomView = bottomView;
[bottomView setFrame:CGRectMake(40, SCREEN_HEIGHT, SCREEN_WIDTH-80, SCREEN_HEIGHT/2)];
//加一个取消的按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.cancelButton = button;
[button setFrame:CGRectMake(SCREEN_WIDTH-40-40, -40, 40, 40)];
[self setFrame:(CGRect){0, 0, SCREEN_WIDTH,SCREEN_HEIGHT}];
[self.backWindow addSubview:self];
}
return self;
}
#pragma mark - 点击按钮取消
-(void)clickButton:(UIButton *)button{
[self dismiss];
}
#pragma mark - 设置window
- (UIWindow *)backWindow {
if (_backWindow == nil) {
_backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_backWindow.windowLevel = UIWindowLevelStatusBar;
_backWindow.backgroundColor = [UIColor clearColor];
_backWindow.hidden = NO;
}
return _backWindow;
}
- (void)dismiss:(UITapGestureRecognizer *)tap {
[self dismiss];
}
#pragma mark - 取消动画
-(void)dismiss{
[UIView animateWithDuration:0.3f
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.drakView setAlpha:0];
[self.drakView setUserInteractionEnabled:NO];
CGRect frame = _bottomView.frame;
frame.origin.y = SCREEN_HEIGHT;
[_bottomView setFrame:frame];
CGRect cancelFrame = _cancelButton.frame;
cancelFrame.origin.y = -40;
[_cancelButton setFrame:cancelFrame];
}
completion:^(BOOL finished) {
_backWindow.hidden = YES;
[self removeFromSuperview];
}];
}
#pragma mark - 弹窗
- (void)show {
_backWindow.hidden = NO;
[UIView animateWithDuration:.75f
delay:0.2
usingSpringWithDamping:0.65f
initialSpringVelocity:0.5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.drakView setAlpha:0.4f];
[self.drakView setUserInteractionEnabled:YES];
CGRect frame = _bottomView.frame;
frame.origin.y = (SCREEN_HEIGHT-frame.size.height)/2;
[_bottomView setFrame:frame];
CGRect cancelFrame = _cancelButton.frame;
cancelFrame.origin.y = frame.origin.y/2;
[_cancelButton setFrame:cancelFrame];
}
completion:^(BOOL finished) {
}];
}
@end
剩下的想必大家都懂了!如有问题,可以大家讨论讨论,不喜勿喷!
END.
网友评论