在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,273评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,126评论 25 709
  • 细雨蒙蒙,窗外飘来一缕草的味道,清香扑鼻;心绪荡漾,内心涌来一股爱的味道,暖人肺腑。 ...
    给我一个留下的理由阅读 310评论 0 0
  • 风花雪月系列 蝶国嫣然在魔蓝大军大举进攻之时,国主连同长老与扶风、望月、催花,御雪四位祭司之力,冰封嫣然,等待时机...
    柳青陵阅读 847评论 0 1