iOS为所需要的视图添加模糊效果--UIVisualEffectView

使用到的类: UIVisualEffectView (iOS8 later)
使用方法:在需要的效果的视图上再添加一层这个UIVisualEffectView子视图即可,这个子视图上也可以添加视图,但是不可直接添加,需要放在UIVisualEffectView contentView
注意点:往往这种效果是由一个点击事件触发的,将代码写在点击事件的方法中即可
 
代码示例:
   
   UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];//Style 可选
    UIVibrancyEffect *vibrancy = [UIVibrancyEffect effectForBlurEffect:effect];
   
    UIVisualEffectView *visualEff = [[UIVisualEffectView alloc] initWithEffect:effect];
    visualEff.frame = CGRectMake(0, 100, 320, 30);
    visualEff.backgroundColor = [UIColor greenColor];//为了现实效果,加个背景色
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, visualEff.frame.size.width, visualEff.frame.size.height)];
    label.text = @"blur";
    label.textAlignment = NSTextAlignmentCenter;
    [visualEff.contentView addSubview:label];//不能直接将子视图放在对象上
    [self.view addSubview:visualEff];//self.view 可以替换成你任何想使之虚化的视图

效果图:
 
 
posted @ 2015-01-09 15:00  Phzean  阅读(441)  评论(0编辑  收藏  举报