在Window上加一层

在keyWindow上添加UIView,可以实现类似微信支付的控件。

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:self];
//
//  WindowView.m
//  Window
//
//  Created by lei_dream on 16/3/25.
//  Copyright © 2016年 lei_dream. All rights reserved.
//

#import "WindowView.h"

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kLabelWidth 100.0f
#define kLabelHeight 40.0f

@implementation WindowView

-(instancetype)init{
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3f];
        
        [self drawView];
    }
    return self;
}

-(void)drawView{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth-kLabelWidth)/2, (kScreenHeight-kLabelHeight)/2, kLabelWidth, kLabelHeight)];
    label.text = @"UIVIEW";
    label.textColor = [UIColor blueColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((kScreenWidth-20)/2, (kScreenHeight-kLabelHeight)/2 + 60, 20, 20)];
    [button setTitle:@"X" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 10;
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor blueColor].CGColor;
    [button addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

-(void)show{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:self];
    self.alpha = 0;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

-(void)dismiss{
    self.alpha = 1;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }];
    [self removeFromSuperview];
}
@end

你可能感兴趣的:(在Window上加一层)