美文网首页IOS 实战项目首页投稿(暂停使用,暂停投稿)iOS Developer
IOS实战 (4) 之 仿微信新闻评论框+ 半透明模糊效果

IOS实战 (4) 之 仿微信新闻评论框+ 半透明模糊效果

作者: 移动开发者_李挺哲 | 来源:发表于2016-08-18 16:21 被阅读582次

效果图##


网易新闻评论图

网易新闻评论

仿图

这里写图片描述

实现思路##


1.键盘弹出事件
2.UIVisualEffectView作为背景图

核心代码##


1.键盘弹出事件监听
添加通知监听

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

实现 keyboardWillShow 方法(获取键盘高度然后针对 View 进行调整 )

 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];

创建模糊图层

 
    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;
        
    }

总结##


整个例子非常简单,模糊效果 IOS8 及以上 可以使用UIVisualEffectView 就可以实现官方的模糊效果. IOS 7 以上可以用 ToolBar 勉强代替.要是还要向下兼容.我的源码里有一个大神做的 BlurView 也可以使用.

源码##


源码下载

相关文章

网友评论

  • bd08df4f32d0:能加下我吗1044630716(好友问题随变写就行) 请教您点问题 :pray: :pray:

本文标题: IOS实战 (4) 之 仿微信新闻评论框+ 半透明模糊效果

本文链接:https://www.haomeiwen.com/subject/guhpsttx.html