項目中又做到下圖的個人頁界面,又要重新開始解決各個控件重疊觸發的問題,決定加蒙版,然后又發現導航欄沒被遮住,就決定加載Window上,沒什么難得東西,只是為了以后直接拿來用罷了
WechatIMG20.jpeg
直接上代碼,加上注釋應該很清楚了
@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];
}