毛玻璃效果与渐变

毛玻璃效果与渐变,第1张

UIToolbar有一个属性:barStyle,设置对应的枚举值来呈现毛玻璃的样式,最后再添加到需要进行毛玻璃效果的view上即可.

观察UIToolbar的试图结构:UIBarBackgournd、UIVisualEffectView、UIVisualEffectFilterView

在iOS8.0之后,苹果新增了一个类UIVisualEffectView,通过UIVisualEffectView即可实现毛玻璃效果。

UIVisualEffectView在初始化时,需要一个UIVisualEffect参数。另外UIVisualEffect是一个抽象类,需通过它下面的子类来实现。UIBlurEffect, UIVibrancyEffect。

渐变可通过 CAGradientLayer 实现。只需设置其颜色数组、起点与终点。

demo地址: https://github.com/taoGod/frostedGlass

如何通过 ToolBar 模拟出图片的毛玻璃效果。首先我们新建一个工程,工程模板切换到 iOS ,选择 Single View Application ,如下图所示:

点击 Next ,命名任意,Language 选择 Objective-C,如下图所示:

输入完工程名之后,继续点击 Next ,选择一个在你 Mac 上用于存储工程文件的目录完成即可,工程建立好之后开始我们今天的代码之旅。

既然需要模拟图片的毛玻璃效果,我们首先当然就需要一张图片,我这里用了一张周杰伦第一张专辑的封面作为本节内容的图片素材,图片如下:

接下来,将这张图片拖动到刚才建立好的工程文件的文件夹 Assets.xcassets 中,如下图所示:

现在点击左侧文件列表选择 ViewController.m 文件,这时候会在文件列表右侧代码区域显示相关代码,接下来我们书写代码,完成我们今天想要达到的效果。

需要注意的是,以下代码我们需要写在 viewDidLoad 方法中,OK,开始。

第一步,我们首先对图片进行处理,将图片添加进 iPhone 界面,并全屏显示。相关代码如下:

创建 UIImageView 对象用于存储图片,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">UIImageView *imageView = <span style="color:#999988font-style:italic">[<span style="">[UIImageView alloc]</span>init]</span></code></pre>

设置图片尺寸占据整个屏幕,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">imageView.frame = <span style="font-weight:700">self</span>.view.bounds</code></pre>

指定待显示图片资源,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">imageView.image = [UIImage <span style="color:#dd1144">imageNamed:</span>@<span style="color:#dd1144">"Jay.jpg"</span>]</code></pre>

设置图片显示模式,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">imageView.contentMode = UIViewContentModeScaleToFill<span style="color:#999988font-style:italic"></span></code></pre>

在 iPhone 上显示 image 图片,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal"><span style="color:#9900font-weight:700">[self.view addSubview:imageView]</span><span style="color:#999988font-style:italic"></span></code></pre>

先来看一下现在效果如何,运行模拟器,效果如下:

这样我们就将这张图片添加到了 iPhone ,并进行了全屏显示,由于图片比例和 iPhone 比例不一致,所以这里略显比例失衡,大家可以找一张比例一致的图片进行测试,接下来我们看下一步如何制作毛玻璃效果 。

第二步,制作毛玻璃效果,这里我们利用 ToolBar 覆盖在图片上来模拟毛玻璃的效果,具体 *** 作方法就是先建立一个 ToolBar 对象,然后设置它的尺寸和图片尺寸一致,即设置为屏幕尺寸,然后覆盖在图片上即可,相关 *** 作代码如下。

创建 ToolBar 对象,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">UIToolbar *toolBar = <span style="color:#999988font-style:italic">[<span style="">[UIToolbar alloc]</span>init]</span></code></pre>

设置 ToolBar 尺寸和图片尺寸一致,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">toolBar.frame = imageView.bounds<span style="color:#999988font-style:italic"></span></code></pre>

设置毛玻璃效果,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">toolBar.barStyle = UIBarStyleBlack<span style="color:#999988font-style:italic"></span></code></pre>

将 toolBar 添加到图片上覆盖图片,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal"><span style="color:#9900font-weight:700">[imageView addSubview:toolBar]</span><span style="color:#999988font-style:italic"></span></code></pre>

运行模拟器,效果如下:

这是黑色的毛玻璃效果,关于毛玻璃还有一种默认的白色效果,只需将毛玻璃效果设置代码修改为如下代码即可:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">toolBar.barStyle = UIBarStyleDefault<span style="color:#999988font-style:italic"></span></code></pre>

运行模拟器,效果如下:

但个人觉得黑色要更加好看一些,所以这里我使用黑色,我将设置代码还原为黑色效果。

现在,图片已经出现了毛玻璃效果,比较模糊,如果这时候我们在毛玻璃上写字,那么这个字一定会异常清晰,要不要试试看,OK,我们来继续在毛玻璃上面写点字看一下效果,我们就在图片下方写上这张专辑的名字吧。

第三步,添加文字,相关代码如下。

创建 Label 对象,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">UILabel *label = <span style="color:#999988font-style:italic">[<span style="">[UILabel alloc]</span>init]</span></code></pre>

设置文字显示位置,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">label.frame = CGRectMake(self.view.frame.size.width/<span style="color:teal">2</span>-<span style="color:teal">50</span>, self.view.frame.size.height/<span style="color:teal">2</span>+<span style="color:teal">150</span>, <span style="color:teal">100</span>, <span style="color:teal">100</span>)</code></pre>

设置文字显示颜色为白色,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">label.textColor = <span style="color:#dd1144">[UIColor whiteColor]</span></code></pre>

设置文字居中对齐,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal"><span style="font-weight:700">label</span>.textAlignment = NSTextAlignmentCenter</code></pre>

设置文字显示内容,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">label.<span style="color:#445588font-weight:700">text</span>= @<span style="color:#dd1144">"Jay"</span></code></pre>

将文字添加到 toolBar 上进行显示,代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal"><span style="color:#9900font-weight:700">[toolBar addSubview:label]</span><span style="color:#999988font-style:italic"></span></code></pre>

运行模拟器,效果如下:

至此,全部代码写完,完成今天毛玻璃效果图片的效果演示,完整代码如下:

<pre style="font-size:11.899999618530273pxline-height:1.45font-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-wrap:normalpadding:16pxoverflow:autobackground-color:rgb(247,247,247)color:rgb(51,51,51)margin-top:0px!importantmargin-bottom:0px!important"><code style="display:inlineoverflow:visiblebackground-color:transparentfont-family:Consolas,'Liberation Mono',Menlo,Courier,monospaceword-break:normalborder:0pxline-height:inheritword-wrap:normal">- (<span style="font-weight:700">void</span>)viewDidLoad { &nbsp&nbsp[<span style="font-weight:700">super</span>viewDidLoad]&nbsp&nbsp&nbsp&nbsp<span style="color:#999988font-style:italic">// 第一步:对图片进行处理</span>&nbsp&nbsp<span style="color:#086b3">UIImageView</span>*imageView = [[<span style="color:#086b3">UIImageView</span>alloc]init]&nbsp&nbspimageView<span style="color:teal">.frame</span>= <span style="font-weight:700">self</span><span style="color:teal">.view</span><span style="color:teal">.bounds</span>&nbsp&nbspimageView<span style="color:teal">.image</span>= [<span style="color:#086b3">UIImage</span>imageNamed:<span style="color:#dd1144">@"Jay.jpg"</span>]&nbsp&nbspimageView<span style="color:teal">.contentMode</span>= <span style="color:#086b3">UIViewContentModeScaleToFill</span>&nbsp&nbsp&nbsp&nbsp<span style="color:#999988font-style:italic">// 第二步:制作毛玻璃</span>&nbsp&nbsp<span style="color:#086b3">UIToolbar</span>*toolBar = [[<span style="color:#086b3">UIToolbar</span>alloc]init]&nbsp&nbsptoolBar<span style="color:teal">.frame</span>= imageView<span style="color:teal">.bounds</span>&nbsp&nbsptoolBar<span style="color:teal">.barStyle</span>= <span style="color:#086b3">UIBarStyleBlack</span>&nbsp&nbsp[imageView addSubview:toolBar]&nbsp&nbsp&nbsp&nbsp<span style="color:#999988font-style:italic">// 第三步:添加文字</span>&nbsp&nbsp<span style="color:#086b3">UILabel</span>*label = [[<span style="color:#086b3">UILabel</span>alloc]init]&nbsp&nbsplabel<span style="color:teal">.frame</span>= <span style="color:#086b3">CGRectMake</span>(<span style="font-weight:700">self</span><span style="color:teal">.view</span><span style="color:teal">.frame</span><span style="color:teal">.size</span><span style="color:teal">.width</span>/<span style="color:teal">2</span>-<span style="color:teal">50</span>, <span style="font-weight:700">self</span><span style="color:teal">.view</span><span style="color:teal">.frame</span><span style="color:teal">.size</span><span style="color:teal">.height</span>/<span style="color:teal">2</span>+<span style="color:teal">150</span>, <span style="color:teal">100</span>, <span style="color:teal">100</span>)&nbsp&nbsplabel<span style="color:teal">.textColor</span>= [<span style="color:#086b3">UIColor</span>whiteColor]&nbsp&nbsplabel<span style="color:teal">.textAlignment</span>= <span style="color:#086b3">NSTextAlignmentCenter</span>&nbsp&nbsplabel<span style="color:teal">.text</span>= <span style="color:#dd1144">@"Jay"</span>&nbsp&nbsp[toolBar addSubview:label]&nbsp&nbsp&nbsp&nbsp[<span style="font-weight:700">self</span><span style="color:teal">.view</span>addSubview:imageView]}</code></pre>

所谓的毛玻璃效果其实是半透明+模糊,可以用CSSfilter实现。时间不多,就抛个砖,题主可以自己找一下文档和案例。filter-CSSdiv{-moz-filter:blur(5px)-webkit-filter:blur(5px)-o-filter:blur(5px)-ms-filter:blur(5px)filter:blur(5px)}大体思路是,当出现d出层的时候,对背景层增加blurfilter,再给d出层的一定透明度就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11673047.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存