美文网首页
iOS-毛玻璃效果

iOS-毛玻璃效果

作者: iSongWei | 来源:发表于2016-10-12 09:53 被阅读109次

iOS8之后苹果提供了制作毛玻璃效果的API

就是这个UIVisualEffectView,用这个initWithEffect:去初始化,然后呢,他有两种效果继承自UIVisualEffect。这个父类不用管,什么也不做,主要看他的两个子类UIBlurEffect和UIVibrancyEffect。

UIBlurEffect : 这个是影响毛玻璃后面视图的

效果图:


1.png

UIVibrancyEffect : 这个是影响毛玻璃上的视图的

2.png

是不是很漂亮,做起来也不难呢。

先说毛玻璃下面的效果的做法。

你先初始化一个UIBlurEffect对象,他有三种风格

typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark
}

上面的效果都是用UIBlurEffectStyleLight做出来的

UIBlurEffectStyleExtraLight效果如下

3.png

UIBlurEffectStyleDark效果如下

4.png

然后呢用这个UIBlurEffect对象创建一个UIVisualEffectView对象。用你需要被虚化的视图添加这个UIVisualEffectView对象为子视图就可以了。

接着说毛玻璃上面的效果

创建一个UIVibrancyEffect对象,用之前创建的blur去初始化,然后创建一个UIVisualEffectView对象,用这个UIVibrancyEffect对象初始化,最后将你想要添加的子视图添加到UIVisualEffectView的contentView上就可以了。

完整代码如下:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
imageView.image = [UIImage imageNamed:@"pic"];
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *ev = [[UIVisualEffectView alloc] initWithEffect:blur];
ev.frame = self.view.frame;
[imageView addSubview:ev];
 
// UIVibrancyEffect效果
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blur];
UIVisualEffectView *ano = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
ano.frame = self.view.frame;
 
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:40];
label.frame = CGRectMake(100, 100, 400, 100);
label.text = @"Beautiful View";
[ev.contentView addSubview:ano];
[ano.contentView addSubview:label];
 
[self.view addSubview:imageView];

这里是链接

相关文章

网友评论

      本文标题:iOS-毛玻璃效果

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