自定义pop弹框

2019-07-08  本文已影响0人  nickNic
#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
  ShowTypeTop = 0,
  ShowTypeCenter = 1,
  ShowTypeBottom =2
}ShowType;




 @interface UIView (ZZTPop)



- (void)showView:(UIView*)view WithContentHeight:(CGFloat)contentHeight;

- (void)showView:(UIView *)viewCenter;

- (void)showView:(UIView*)view Type:(ShowType)type;

- (void)close;
@end





#import "UIView+ZZTPop.h"
#import <objc/runtime.h>



#define APPSIZE [[UIScreen mainScreen] bounds].size
#define CANCELHEIGHT 44
static UIView *popTopView;
static UIView *maskView;



@implementation UIView (ZZTPop)


- (void)showView:(UIView *)view WithContentHeight:(CGFloat)contentHeight
{
    if (contentHeight>=(APPSIZE.height- CANCELHEIGHT)) {
        contentHeight =(APPSIZE.height- CANCELHEIGHT);
    }

    if (maskView == nil) {
        maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.400];
        maskView.alpha = 0.0f;
            // 添加点击背景按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = [UIScreen mainScreen].bounds;
        [btn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
        [maskView  addSubview:btn];
    
    }

if (popTopView==nil) {
    popTopView = [[UIView alloc]initWithFrame:CGRectMake(0, APPSIZE.height, APPSIZE.width, contentHeight)];
    popTopView.backgroundColor = [UIColor redColor];
    UIButton *btn_Cancel =[UIButton buttonWithType:UIButtonTypeCustom];
    btn_Cancel.frame = CGRectMake(0,CGRectGetMaxY(maskView.frame)-CANCELHEIGHT,APPSIZE.width , CANCELHEIGHT);
    [btn_Cancel setTitle:@"取消" forState:UIControlStateNormal];
    [btn_Cancel setBackgroundColor:[UIColor yellowColor]];
    [btn_Cancel addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [maskView  addSubview:btn_Cancel];
}

[[UIApplication sharedApplication].keyWindow addSubview:self];
[[UIApplication sharedApplication].keyWindow addSubview:maskView];
[[UIApplication sharedApplication].keyWindow addSubview:popTopView];
[popTopView addSubview:view];

[UIView animateWithDuration:0.25 animations:^{
    maskView.alpha = 1.0;
    popTopView.transform = CGAffineTransformTranslate(popTopView.transform, 0,-      (popTopView.frame.size.height+CANCELHEIGHT));

    } completion:^(BOOL finished) {
//          popTopView.transform = CGAffineTransformTranslate(popTopView.transform, 0,-          (popTopView.frame.size.height+CANCELHEIGHT));
//         popTopView.transform = CGAffineTransformIdentity;
//        [UIView animateWithDuration:0.25 animations:^{
//                popTopView.transform = CGAffineTransformTranslate(popTopView.transform, 0,-    (popTopView.frame.size.height+CANCELHEIGHT));
//        }];
    }];

NSLog(@"change world");
}

- (void)showView:(UIView *)viewCenter
{

  if (maskView == nil) {
    maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    maskView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.400];
    maskView.alpha = 0.0f;
    // 添加点击背景按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = [UIScreen mainScreen].bounds;
    [btn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [maskView  addSubview:btn];
    
}

if (popTopView==nil) {
    popTopView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    popTopView.backgroundColor = [UIColor redColor];
    popTopView.center = maskView.center;
    UIButton *btn_Cancel =[UIButton buttonWithType:UIButtonTypeCustom];
    btn_Cancel.frame = CGRectMake(0,CGRectGetMaxY(maskView.frame)-CANCELHEIGHT,APPSIZE.width , CANCELHEIGHT);
    [btn_Cancel setTitle:@"取消" forState:UIControlStateNormal];
    [btn_Cancel setBackgroundColor:[UIColor yellowColor]];
    [btn_Cancel addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [maskView  addSubview:btn_Cancel];
}

[[UIApplication sharedApplication].keyWindow addSubview:self];
[[UIApplication sharedApplication].keyWindow addSubview:maskView];
[[UIApplication sharedApplication].keyWindow addSubview:popTopView];
[popTopView addSubview:viewCenter];

[UIView animateWithDuration:0.25 animations:^{
    maskView.alpha = 1.0;
    popTopView.transform = CGAffineTransformMakeScale(1.1, 1.1);
 //        popTopView.transform = CGAffineTransformTranslate(popTopView.transform, 0,-  (popTopView.frame.size.height+CANCELHEIGHT));
        //        [UIView animateWithDuration:0.25 animations:^{
        //              popTopView.transform = CGAffineTransformTranslate(popTopView.transform, 0,-   (popTopView.frame.size.height+CANCELHEIGHT));
        //        }];
    } completion:^(BOOL finished) {
        //          popTopView.transform = CGAffineTransformTranslate(popTopView.transform, 0,-  (popTopView.frame.size.height+CANCELHEIGHT));
        //         popTopView.transform = CGAffineTransformIdentity;
                [UIView animateWithDuration:0.25 animations:^{
                        popTopView.transform = CGAffineTransformMakeScale(1, 1);
                }];
        }];
}

- (void)showView:(UIView *)view Type:(ShowType)type
{

switch (type) {
    case ShowTypeTop:
        
        break;
    case ShowTypeCenter:
        
        break;
    case ShowTypeBottom:
        
        break;

}
}
  - (void)close
{

// 关闭顶部视图动画
[UIView animateWithDuration:0.3 animations:^{
    maskView.alpha = 0.0;
    popTopView.transform = CGAffineTransformIdentity;
}completion:^(BOOL finished) {
    [maskView removeFromSuperview];
    [popTopView removeFromSuperview];
    maskView = nil;
    popTopView = nil;
//        [self removeFromSuperview];
}];
}


@end
上一篇下一篇

猜你喜欢

热点阅读