项目中又做到下图的个人页界面,又要重新开始解决各个控件重叠触发的问题,决定加蒙版,然后又发现导航栏没被遮住,就决定加载Window上,没什么难得东西,只是为了以后直接拿来用罢了
直接上代码,加上注释应该很清楚了
@property (nonatomic,strong) AppDelegate *appdelegate;
@property (nonatomic,strong) UIView *backgroundView; // 蒙版
// 懒加载
- (UIView *)backgroundView {
if (!_backgroundView) {
_backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_backgroundView.backgroundColor = [UIColor clearColor];
}
return _backgroundView;
}
- (void)viewDidLoad {
[super viewDidLoad];
_appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)buttonClick:(UIButton*)button {
// 添加手势到window 点击还原
UITapGestureRecognizer *tapLeftDouble = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage:)];
[_appdelegate.window addGestureRecognizer:tapLeftDouble];
[_appdelegate.window addSubview:self.backgroundView]; // 放置蒙版
// 改变蒙版颜色及透明度
[UIView animateWithDuration:.15f animations:^{
self.backgroundView.backgroundColor = RGB(0, 0, 0);
self.backgroundView.alpha = .4f;
}];
// 动画改变目标view上升速度
CATransition *animation = [CATransition animation];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[animation setDuration:.15f];
[animation setType:kCATransitionMoveIn];
[animation setSubtype: kCATransitionFromTop];
// _dateView 是我使用的view 此处替换成自己的
_dateView = [SHDatePickerView viewFromXIB];
_dateView.frame = CGRectMake(0, viewHeight - 261, viewWidth, 261);
[_appdelegate.window addSubview:_dateView];
[_dateView.layer addAnimation:animation forKey:@"Reveal"];
}
// 手势还原
-(void)magnifyImage:(UIGestureRecognizer *)gesture
{
[_dateView removeFromSuperview];
[self.backgroundView removeFromSuperview];
}