美文网首页
在Window上加一层

在Window上加一层

作者: Windream | 来源:发表于2016-03-25 16:33 被阅读39次

    在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上加一层

          本文链接:https://www.haomeiwen.com/subject/glcdlttx.html