毛玻璃

UIBlurEffect

// 对应下图的效果
typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {
    UIBlurEffectStyleExtraLight, // 额外亮
    UIBlurEffectStyleLight,      // 亮
    UIBlurEffectStyleDark,       // 黑色
    UIBlurEffectStyleExtraDark,  // 额外黑,iOS上不能用
    UIBlurEffectStyleRegular,    // 常规
    UIBlurEffectStyleProminent,  // 适应用户界面风格
} NS_ENUM_AVAILABLE_IOS(8_0);

效果

/* 
 UIBlurEffect将提供一个模糊的效果,它似乎已经应用于UIVisualEffectView后面的内容层。
 添加到contentView的视图具有模糊的视觉效果,不会使自身模糊。
*/
NS_CLASS_AVAILABLE_IOS(8.0) @interface UIBlurEffect : UIVisualEffect

/* 初始化一个UIBlurEffect对象,并指定模糊效果 */
+ (UIBlurEffect *)effectWithStyle:(UIBlurEffectStyle)style;

@end

示例

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.frame = self.view.bounds;
    imageView.image = [UIImage imageNamed:@"wallpaper"];
    [self.view addSubview:imageView];

    UIVisualEffectView * extraLightView = [self effectViewFromStyle:UIBlurEffectStyleExtraLight];
    [imageView addSubview:extraLightView];
    
    UIVisualEffectView * lightView = [self effectViewFromStyle:UIBlurEffectStyleLight];
    [imageView addSubview:lightView];
 
    UIVisualEffectView * darkView = [self effectViewFromStyle:UIBlurEffectStyleDark];
    [imageView addSubview:darkView];
    
    UIVisualEffectView * regularView = [self effectViewFromStyle:UIBlurEffectStyleRegular];
    [imageView addSubview:regularView];
    
    UIVisualEffectView * prominentView = [self effectViewFromStyle:UIBlurEffectStyleProminent];
    [imageView addSubview:prominentView];
}

- (UIVisualEffectView *)effectViewFromStyle:(UIBlurEffectStyle)style{
    
    CGRect rect = self.view.bounds;
    NSString * labelText = @"";
    NSInteger index = 0;
    switch (style) {
        case UIBlurEffectStyleExtraLight:
            labelText = @"UIBlurEffectStyleExtraLight";
            index = 0;
            break;
        case UIBlurEffectStyleLight:
            labelText = @"UIBlurEffectStyleLight";
            index = 1;
            break;
        case UIBlurEffectStyleDark:
            labelText = @"UIBlurEffectStyleDark";
            index = 2;
            break;
        case UIBlurEffectStyleRegular:
            labelText = @"UIBlurEffectStyleRegular";
            index = 3;
            break;
        case UIBlurEffectStyleProminent:
            labelText = @"UIBlurEffectStyleProminent";
            index = 4;
            break;
        default: break;
    }
    
    // 创建UIBlurEffect类的对象blur
    UIBlurEffect *blurForHeadImage = [UIBlurEffect effectWithStyle:style];
    
    // 创建UIVisualEffectView的对象visualView, 以blur为参数
    UIVisualEffectView * visualViewForHeadImage = [[UIVisualEffectView alloc] initWithEffect:blurForHeadImage];
    visualViewForHeadImage.layer.cornerRadius = 10;
    visualViewForHeadImage.clipsToBounds = YES;
    visualViewForHeadImage.frame = CGRectMake(rect.size.width * 0.05,
                                              20 + index * ((rect.size.height - 100) * 0.2 + 20),
                                              rect.size.width * 0.9,
                                              (rect.size.height - 100 - 80) * 0.2);
    
    UILabel * label = [UILabel new];
    label.frame = visualViewForHeadImage.bounds;
    label.textAlignment = NSTextAlignmentCenter;
    [visualViewForHeadImage.contentView addSubview:label];
    label.text = labelText;

    
    return visualViewForHeadImage;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值