Swift-圆角与阴影

作者: FlyElephant | 来源:发表于2017-03-26 14:10 被阅读1749次

iOS中圆角实现非常容易,对比而言,设置阴影则需要设置颜色,偏移位置,阴影透明度,阴影半径:
<pre><code>` /* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */

/** Shadow properties. **/
open var shadowColor: CGColor?


/* The opacity of the shadow. Defaults to 0. Specifying a value outside the
 * [0,1] range will give undefined results. Animatable. */

open var shadowOpacity: Float


/* The shadow offset. Defaults to (0, -3). Animatable. */

open var shadowOffset: CGSize


/* The blur radius used to create the shadow. Defaults to 3. Animatable. */

open var shadowRadius: CGFloat`</code></pre>
FlyElephant.png

图一实现的代码非常原始,简单设置了阴影颜色:
<pre><code>` let view:UIView = UIView.init(frame: CGRect(x: 50, y: 200, width: 50, height: 50))

    view.backgroundColor = UIColor.blue
    
    view.layer.shadowColor = UIColor.red.cgColor
    view.layer.shadowOpacity = 1.0
    
    self.view.addSubview(view)`</code></pre>

对比图一阴影分步的更加均匀,shadowOffset都设置了0:
<pre><code>` let view1:UIView = UIView.init(frame: CGRect(x: 150, y: 200, width: 50, height: 50))

    view1.backgroundColor = UIColor.blue
    
    view1.layer.shadowColor = UIColor.red.cgColor
    view1.layer.shadowOpacity = 1.0
    view1.layer.shadowOffset = CGSize(width: 0, height: 0)
    view1.layer.shadowRadius = 4
    
    self.view.addSubview(view1)`</code></pre>

图三是设置阴影,同时设置圆角,阴影不显示:
<pre><code>` let view2:UIView = UIView.init(frame: CGRect(x: 250, y: 200, width: 50, height: 50))

    view2.backgroundColor = UIColor.blue
    view2.layer.masksToBounds = true
    view2.layer.cornerRadius = 25
    
    view2.layer.shadowColor = UIColor.red.cgColor
    view2.layer.shadowOpacity = 1.0
    view2.layer.shadowOffset = CGSize(width: 0, height: 0)
    view2.layer.shadowRadius = 4
    
    self.view.addSubview(view2)`</code></pre>

图四既设置圆角同时设置阴影:

<pre><code>` let view2:UIView = UIView.init(frame: CGRect(x: 260, y: 200, width: 50, height: 50))

    view2.backgroundColor = UIColor.blue
    view2.layer.cornerRadius = 25
    
    view2.layer.shadowColor = UIColor.red.cgColor
    view2.layer.shadowOpacity = 1.0
    view2.layer.shadowOffset = CGSize(width: 0, height: 0)
    view2.layer.shadowRadius = 4
    view2.layer.masksToBounds = false
    
    self.view.addSubview(view2)`</code></pre>

<pre><code>` let shadowView:UIView = UIView(frame: CGRect(x: 50, y: 300, width: 50, height: 50))
shadowView.backgroundColor = UIColor.white
shadowView.layer.shadowColor = UIColor.red.cgColor
shadowView.layer.shadowOpacity = 1.0
shadowView.layer.shadowOffset = CGSize(width: 0, height: 0)
shadowView.layer.shadowRadius = 4
shadowView.clipsToBounds = false
shadowView.layer.cornerRadius = 25.0

    let innerView:UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    
    innerView.backgroundColor = UIColor.yellow
    innerView.clipsToBounds = true
    innerView.layer.cornerRadius = 25
    
    shadowView.addSubview(innerView)
    
    self.view.addSubview(shadowView)`</code></pre>

相关文章

  • Swift-圆角与阴影

    iOS中圆角实现非常容易,对比而言,设置阴影则需要设置颜色,偏移位置,阴影透明度,阴影半径: ` /* T...

  • css3

    圆角与阴影 圆角: 阴影: 怎样的阴影: 文字 长单词允许长单词、URL强制进行换行 自定义 字体: 2D 转换 ...

  • 圆角阴影

    全圆角阴影 部分圆角阴影 swift 版

  • 圆角与阴影

    1. 画圆角 (1)传统的圆角设置方式 imageView.clipsToBounds = YES; imag...

  • iOS imgView设置圆角与阴影

    圆角与阴影有冲突不可共存 常规设置圆角_imgView makeRadius:_imgView.hight/2;(...

  • Swift-同时添加圆角和阴影

    自定义View,在init方法中 在layoutSubViews()中 利用这个自定义View创建的视图,都是圆角...

  • 切圆角、圆角与阴影同时存在总结

    一、切圆角 1.使用layer. cornerRadius 2.贝塞尔曲线切 二、圆角与阴影共存 1.设置阴影时圆...

  • 圆角和阴影共存

    圆角和阴影并存: 两个view分别处理圆角和阴影。 处理阴影的view可以是透明的。 圆角如果不用masksToB...

  • CALayer常用属性整理(三)

    圆角与阴影 单纯的圆角应该没什么可说的。网上有很多设置圆角的方式。比如最简单的layer.cornerRadius...

  • 源码推荐:TableviewGroup阴影加圆角 长按拖拽排序

    源码推荐:TableviewGroup阴影加圆角 长按拖拽排序 源码推荐:TableviewGroup阴影加圆角 ...

网友评论

本文标题:Swift-圆角与阴影

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