在Window上加一層

在keyWindow上添加UIView,可以實現(xiàn)類似微信支付的控件。

    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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,304評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,034評論 25 709
  • 細雨蒙蒙,窗外飄來一縷草的味道,清香撲鼻;心緒蕩漾,內心涌來一股愛的味道,暖人肺腑。 ...
    給我一個留下的理由閱讀 335評論 0 0
  • 風花雪月系列 蝶國嫣然在魔藍大軍大舉進攻之時,國主連同長老與扶風、望月、催花,御雪四位祭司之力,冰封嫣然,等待時機...
    柳青陵閱讀 1,037評論 0 1

友情鏈接更多精彩內容