iOS--弹出视图后使window背景变暗

实现思路解析:

1,首先要拿到window (方式有多重可以app delegate,或者创建window、keywindow等等方式)

2, 然后创建一个backgroundView,使其frame和window相等,设置背景颜色,再添加到window上。

3,把需要显示的view添加到backgroundView上,当然有动画效果更好

下边的 “弹出视图”“关闭视图”就是对显示在window上的视图的操作

1. 弹出视图

/**显示弹出视图*/
- (void)show{
//1. 取出window
    UIWindow * window = [[UIApplication sharedApplication] keywindow]; 
    //[[[UIApplication sharedApplication] windows] firstObject];有时这样取window会加载不出我们需要的试图,这是因为不是keywindow,比如用storyboard画的试图,推荐用keywindow不论什么情况包不会出问题。
   //2. 创建背景视图
    _bgView = [[UIView alloc]init];
    _bgView.frame = window.bounds;
    //3. 背景颜色可以用多种方法看需要咯
     _bgView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
//    _bgView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.6];
    [window addSubview:_bgView];
    //4. 把需要展示的控件添加上去
    [window addSubview:self];
    //5. 动画简单(low)
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
}

2. 关闭视图

/**关闭弹出视图*/
- (void) hidden {
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.transform = CGAffineTransformMakeScale(0.001, 0.002);
    } completion:^(BOOL finished) {
        [_pickerView removeFromSuperview];
        _pickerView = nil;
        [self removeFromSuperview];
        [_bgView removeFromSuperview];
        _bgView = nil;
    }];
}

下边是我简单封装的PickerView

以上是我简单封装的PickerView的显示,下边是具体的demo也可以记录一下,具体代码见下:

1. pickerView.h文件

#import 

typedef void(^selectedTitle)(NSString *indexTitle);

@interface LLPIckerView : UIView

@property (nonatomic, copy) selectedTitle selectedBlock;
/*
*      内部自行设置了frame,如果需要使传入的frame生效需要自行修改
**     rowCount:表示有多少行,这里只作为显示有多少人
***    点击确认后传出选择多少人
*/
- (LLPIckerView *)initWithFrame:(CGRect)pickerViewFrame rowCount:(NSInteger)rowCount withSelectedComplete:(selectedTitle)selecteBlock;

//显示
- (void)show;
@end

pickerView.m

#import "LLPIckerView.h"

#define margin 40
#define pickerViewWith [UIScreen mainScreen].bounds.size.width - 2*margin
#define pickerViewHeight 260

@interface LLPIckerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation LLPIckerView
{
    UIPickerView * _pickerView;//滚筒
    NSString     * _selecteTitel;//记录选中人数
    UIView       * _bgView;//大背景
}
//初始化数据源数组
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

/**构建*/
- (LLPIckerView *)initWithFrame:(CGRect)pickerViewFrame rowCount:(NSInteger)rowCount withSelectedComplete:(selectedTitle)selecteBlock{
    if (self = [super initWithFrame:pickerViewFrame]) {
        //忽略传过来的frame
        self.layer.cornerRadius = 5;
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, pickerViewWith, pickerViewHeight)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        [self addSubview:_pickerView];
//确认按钮
        UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        confirmButton.frame = CGRectMake(pickerViewWith - 100, pickerViewHeight, 80, margin);
        [confirmButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [self addSubview:confirmButton];
//取消按钮
        UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelButton.frame = CGRectMake(20, pickerViewHeight, 80, margin);
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [self addSubview:cancelButton];
        self.frame = CGRectMake(0, 0, pickerViewWith, pickerViewHeight + margin);
        self.center = [[UIApplication sharedApplication] keyWindow].center;
        self.backgroundColor = [UIColor whiteColor];
    }
//提示标签
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, pickerViewWith, 20)];
    titleLabel.font = [UIFont systemFontOfSize:18];
    [self addSubview:titleLabel];
    if (rowCount>0) {
        //这里仅传入数字  表示有多少人
        titleLabel.text = [NSString stringWithFormat:@"当前餐桌最多可坐%ld人!",rowCount];
        for (int i = 0; iself.dataArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
     _selectedBlock = selecteBlock;//保存block
    return self;
}
/**确认按钮被点击*/
- (void)confirmButtonClick{
    if (self.selectedBlock) {
        if (!_selecteTitel) {
            _selecteTitel = @"0";
        }
        self.selectedBlock(_selecteTitel);
    }
    [self hidden];
}
#pragma mark - pickerView delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
   return self.dataArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _selecteTitel = self.dataArray[row];
}
/**显示弹出视图*/
- (void)show{
    UIWindow * window = [[UIApplication sharedApplication] keywindow];
    _bgView = [[UIView alloc]init];
    _bgView.frame = window.bounds;
     _bgView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
//    _bgView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.6];
    [window addSubview:_bgView];
    [window addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
}

/**关闭弹出视图*/
- (void) hidden {
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.transform = CGAffineTransformMakeScale(0.001, 0.002);
    } completion:^(BOOL finished) {
        [_pickerView removeFromSuperview];
        _pickerView = nil;
        [self removeFromSuperview];
        [_bgView removeFromSuperview];
        _bgView = nil;
    }];
}
@end

ViewController中

#import "ViewController.h"
#import "LLPIckerView.h"
@interface ViewController ()
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(200, 200, 50, 30);
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

}
- (void)buttonClick{
    LLPIckerView * picker = [[LLPIckerView alloc]initWithFrame:CGRectZero rowCount:10 withSelectedComplete:^(NSString *indexTitle) {
    NSLog(@"选中的title:%@",indexTitle);
    }];
    [picker show];
}
@end

你可能感兴趣的:(iOS--视图控件,iOS--常用代码块)