在iOS8.0之后,苹果新增了一个类UIVisualEffectView,通过这个类来实现毛玻璃效果,效率也非常之高,使用也是非常简单,几行代码搞定. UIVisualEffectView是一个抽象类,不能直接使用,需通过它下面的三个子类来实现(UIBlurEffect, UIVisualEffevt, UIVisualEffectView);
子类UIBlurEffect只有一个类方法,用来快速创建一个毛玻璃效果,参数是一个枚举,用来设置毛玻璃的样式,而UIVisualEffectView则多了两个属性和两个构造方法,用来快速将创建的毛玻璃添加到这个UIVisualEffectView上.
UIImageView *textImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, IPHONE_W, IPHONE_W)];
textImage.backgroundColor = [UIColor cyanColor];
textImage.image = [UIImage imageNamed:@"WeChat_1483500208.jpeg"];
[self.view addSubview:textImage];
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = CGRectMake(0, 0, IPHONE_W, IPHONE_W/2);
[textImage addSubview:effectView];
typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark,
UIBlurEffectStyleExtraDark __TVOS_AVAILABLE(10_0) __IOS_PROHIBITED __WATCHOS_PROHIBITED,
UIBlurEffectStyleRegular NS_ENUM_AVAILABLE_IOS(10_0), // Adapts to user interface style
UIBlurEffectStyleProminent NS_ENUM_AVAILABLE_IOS(10_0), // Adapts to user interface style
} NS_ENUM_AVAILABLE_IOS(8_0);
UIBlurEffect 样式展现
UIBlurEffectStyleExtraLight
UIBlurEffectStyleLight
UIBlurEffectStyleDark
iOS 8之前我们会选择用UIToolbar实现
创建一个UIToolbar实例,设置它的frame或者也可以通过添加约束
然后UIToolbar有一个属性:barStyle,设置对应的枚举值来呈现毛玻璃的样式,最后再添加到需要进行毛玻璃效果的view上即可.
UIImageView *textImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, IPHONE_W, IPHONE_W)];
textImage.backgroundColor = [UIColor cyanColor];
textImage.image = [UIImage imageNamed:@"WeChat_1483500208.jpeg"];
[self.view addSubview:textImage];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, IPHONE_W, IPHONE_W/2)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[textImage addSubview:toolbar];
UIBarStyleBlackOpaque
UIBarStyleBlackTranslucent