学猫上吊 發表於 2020-3-5 16:50:00

iOS开发 给同一个view设置四个不同的圆角

<p>1.圆角性能优化</p>
<p>在APP开发中,圆角图片还是经常出现的。如果一个界面中只有少量圆角图片或许对性能没有非常大的影响,但是当圆角图片比较多的时候就会APP性能产生明显的影响。我们设置圆角一般通过如下方式:</p>
<p>imageView.layer.cornerRadius=CGFloat(10);</p>
<p>imageView.layer.masks ToBounds=YES;</p>
<p>这样处理的渲染机制是GPU在当前屏幕缓冲区外新开辟一个 渲染缓冲区进行工作,也就是离屏渲染,这会给我们带来额外的性能损耗,如果这样的圆角操作达到一-定数量, 会触发 缓冲区的频繁合并和上下文的的频繁切换,性能的代价会宏观地表现在用户体验上一一掉帧。</p>
<p>优化方案:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">extension UIView {
   
    </span><span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> BezierPath 圆角设置</span>
    func roundCorners(_ corners: UIRectCorner =<span style="color: rgba(0, 0, 0, 1)"> .allCorners, radius: CGFloat) {
      let maskPath </span>=<span style="color: rgba(0, 0, 0, 1)"> UIBezierPath(
            roundedRect: bounds,
            byRoundingCorners: corners,
            cornerRadii: CGSize(width: radius, height: radius))
      
      let shape </span>=<span style="color: rgba(0, 0, 0, 1)"> CAShapeLayer()
      shape.path </span>=<span style="color: rgba(0, 0, 0, 1)"> maskPath.cgPath
      layer.mask </span>=<span style="color: rgba(0, 0, 0, 1)"> shape
    }
}</span></pre>
</div>
<p>2.设置不同大小的圆角</p>
<p>iOS开发中偶尔也能遇到需要设置不同大小圆角的需求,那么就需要绘制path来实现,具体代码如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">extension UIView {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加4个不同大小的圆角</span>
<span style="color: rgba(0, 0, 0, 1)">    func addCorner(cornerRadii:CornerRadii){
       let path </span>=<span style="color: rgba(0, 0, 0, 1)"> createPathWithRoundedRect(bounds: self.bounds, cornerRadii:cornerRadii)
       let shapLayer </span>=<span style="color: rgba(0, 0, 0, 1)"> CAShapeLayer()
       shapLayer.frame </span>=<span style="color: rgba(0, 0, 0, 1)"> self.bounds
       shapLayer.path </span>=<span style="color: rgba(0, 0, 0, 1)"> path
       self.layer.mask </span>=<span style="color: rgba(0, 0, 0, 1)"> shapLayer
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">各圆角大小</span>
    <span style="color: rgba(0, 0, 255, 1)">struct</span><span style="color: rgba(0, 0, 0, 1)"> CornerRadii {
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> topLeft :CGFloat = <span style="color: rgba(128, 0, 128, 1)">0</span>
      <span style="color: rgba(0, 0, 255, 1)">var</span> topRight :CGFloat = <span style="color: rgba(128, 0, 128, 1)">0</span>
      <span style="color: rgba(0, 0, 255, 1)">var</span> bottomLeft :CGFloat = <span style="color: rgba(128, 0, 128, 1)">0</span>
      <span style="color: rgba(0, 0, 255, 1)">var</span> bottomRight :CGFloat = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">切圆角函数绘制线条</span>
    func createPathWithRoundedRect( bounds:CGRect,cornerRadii:CornerRadii) -&gt;<span style="color: rgba(0, 0, 0, 1)"> CGPath
    {
      let minX </span>=<span style="color: rgba(0, 0, 0, 1)"> bounds.minX
      let minY </span>=<span style="color: rgba(0, 0, 0, 1)"> bounds.minY
      let maxX </span>=<span style="color: rgba(0, 0, 0, 1)"> bounds.maxX
      let maxY </span>=<span style="color: rgba(0, 0, 0, 1)"> bounds.maxY
      
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取四个圆心</span>
      let topLeftCenterX = minX +<span style="color: rgba(0, 0, 0, 1)">cornerRadii.topLeft
      let topLeftCenterY </span>= minY +<span style="color: rgba(0, 0, 0, 1)"> cornerRadii.topLeft
         
      let topRightCenterX </span>= maxX -<span style="color: rgba(0, 0, 0, 1)"> cornerRadii.topRight
      let topRightCenterY </span>= minY +<span style="color: rgba(0, 0, 0, 1)"> cornerRadii.topRight
      
      let bottomLeftCenterX </span>= minX +<span style="color: rgba(0, 0, 0, 1)">cornerRadii.bottomLeft
      let bottomLeftCenterY </span>= maxY -<span style="color: rgba(0, 0, 0, 1)"> cornerRadii.bottomLeft
         
      let bottomRightCenterX </span>= maxX -<span style="color: rgba(0, 0, 0, 1)">cornerRadii.bottomRight
      let bottomRightCenterY </span>= maxY -<span style="color: rgba(0, 0, 0, 1)"> cornerRadii.bottomRight
      
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针</span>
      let path :CGMutablePath =<span style="color: rgba(0, 0, 0, 1)"> CGMutablePath();
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">顶 左</span>
      path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * <span style="color: rgba(128, 0, 128, 1)">3</span> / <span style="color: rgba(128, 0, 128, 1)">2</span>, clockwise: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">顶右</span>
      path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: CGFloat.pi * <span style="color: rgba(128, 0, 128, 1)">3</span> / <span style="color: rgba(128, 0, 128, 1)">2</span>, endAngle: <span style="color: rgba(128, 0, 128, 1)">0</span>, clockwise: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">底右</span>
      path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle: <span style="color: rgba(128, 0, 128, 1)">0</span>, endAngle: CGFloat.pi / <span style="color: rgba(128, 0, 128, 1)">2</span>, clockwise: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">底左</span>
      path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle: CGFloat.pi / <span style="color: rgba(128, 0, 128, 1)">2</span>, endAngle: CGFloat.pi, clockwise: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
      path.closeSubpath();
         </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> path;
    }
}</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/duzhaoquan/p/12421144.html
頁: [1]
查看完整版本: iOS开发 给同一个view设置四个不同的圆角