画个括弧

画了个括弧,分享一下。

import UIKit

class IndicatrixCollectionReusableView: UICollectionReusableView {
    
    var insets:UIEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
    /// 线宽
    var lineWidth:CGFloat = 1
    /// 线颜色
    var strokeColor = UIColor.black
    /// 转角半径
    var radius:CGFloat = 7
    var isLine:Bool = false
    
    override func draw(_ rect: CGRect) {
        guard rect.height >= insets.top + insets.bottom + radius * 2 else{
            fatalError("IndicatrixCollectionReusableView 高度不够。")
        }
        
        let centerX = rect.midX
        let centerY = rect.midY
        let top = insets.top
        let bottom = insets.bottom
        
        
        if insets.left + 2 * radius > centerX || rect.width - (insets.right + 2 * radius) < centerX{
            if !isLine{
                isLine = true
            }
        }

        let linePath = UIBezierPath()
        var point:CGPoint
        
        if isLine{
            point = CGPoint(x: centerX, y: top)
            linePath.move(to: point)
            
            point.y = rect.height - bottom
            linePath.addLine(to: point)
        }else{
            
            point = CGPoint(x: insets.left, y: rect.height - bottom)
            linePath.move(to: point)
            
            
            point.y = rect.height / 2 + radius
            linePath.addLine(to: point)
            
            point.x += radius
            linePath.addArc(withCenter: point, radius: radius, startAngle: CGFloat(M_PI), endAngle: CGFloat(-M_PI_2), clockwise: true)
            
            point.x = centerX - radius
            point.y = centerY
            linePath.addLine(to: point)
            
            point.y -= radius
            linePath.addArc(withCenter: point, radius: radius, startAngle: CGFloat(M_PI_2), endAngle: 0, clockwise: false)
            
            point.x += radius
            point.y = top
            linePath.addLine(to: point)
            
            point.y = centerY - radius
            linePath.addLine(to: point)
            
            point.x += radius
            linePath.addArc(withCenter: point, radius: radius, startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI_2), clockwise: false)
            
            point.y = centerY
            point.x = rect.width - insets.right - radius
            linePath.addLine(to: point)
            
            point.y += radius
            linePath.addArc(withCenter: point, radius: radius, startAngle: CGFloat(-M_PI_2), endAngle: 0, clockwise: true)
            
            point.x += radius
            point.y = rect.height - insets.bottom
            linePath.addLine(to: point)
        }
        linePath.lineJoinStyle = .round
        linePath.lineCapStyle = .round
        linePath.lineWidth = lineWidth
        
        // stroke应该放最后,不然上面的属性设置不起作用
        strokeColor.setStroke()
        linePath.stroke()
    }
}

效果如下:

括弧效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,203评论 25 708
  • 今天京都下大雨,旅馆刚好在西本愿寺旁边,走路3分钟到大门口。本愿寺分东本愿寺和西本愿寺。 1591年丰臣秀吉修建了...
    Hannazhang阅读 306评论 0 1
  • 今日大暑,孩子看电视广告播出后问,妈妈,大暑是不是就是很热很热!我说,是啊!你们学校发了一本关于民俗民风文...
    兆木兆木阅读 102评论 0 1
  • 暑热难熬,工作也如山堆积,在自己疯掉之前,决定开始一场休闲之旅。原计划去苏杭,但是那边的温度跟家里也差不多,我不想...
    无语流沙阅读 433评论 0 2
  • 亲爱的Jeanie, 再过几个小时就是新的一年了。对于你来说,2016年是一个很精彩的一年,所以希望你可以先暂停面...
    jwhavingfun阅读 161评论 0 0