效果圖##
網(wǎng)易新聞評論圖
網(wǎng)易新聞評論
仿圖
這里寫圖片描述
實(shí)現(xiàn)思路##
1.鍵盤彈出事件
2.UIVisualEffectView作為背景圖
核心代碼##
1.鍵盤彈出事件監(jiān)聽
添加通知監(jiān)聽
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
實(shí)現(xiàn) keyboardWillShow 方法(獲取鍵盤高度然后針對 View 進(jìn)行調(diào)整 )
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
NSLog(@"keyboadr %@",NSStringFromCGRect(keyboardBounds));
int keyBoardHeight=keyboardBounds.size.height;
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
dialogView.center=CGPointMake(ScreenWidth/2, ScreenHeight-keyBoardHeight-DialogViewHeight/2);
NSLog(@"dialogView frame %@",NSStringFromCGRect(dialogView.frame));
NSLog(@"comment frame %@",NSStringFromCGRect(commentView.frame));
[UIView commitAnimations];
創(chuàng)建模糊圖層
UIView *maskView;
double version = [[UIDevice currentDevice].systemVersion doubleValue];
if (version >= 8.0f) {
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
maskView = [[UIVisualEffectView alloc] initWithEffect:blur];
((UIVisualEffectView *)maskView).frame = self.view.bounds;
}else if(version >= 7.0f){
maskView = [[UIToolbar alloc] initWithFrame:self.view.bounds];
((UIToolbar *)maskView).barStyle = UIBarStyleDefault;
}
總結(jié)##
整個(gè)例子非常簡單,模糊效果 IOS8 及以上 可以使用UIVisualEffectView 就可以實(shí)現(xiàn)官方的模糊效果. IOS 7 以上可以用 ToolBar 勉強(qiáng)代替.要是還要向下兼容.我的源碼里有一個(gè)大神做的 BlurView 也可以使用.