美文网首页
画个括弧

画个括弧

作者: 轻云绿原 | 来源:发表于2017-04-06 09:51 被阅读6次

画了个括弧,分享一下。

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()
    }
}

效果如下:

括弧效果

相关文章

  • 画个括弧

    画了个括弧,分享一下。 效果如下:

  • PHP json返回大括弧和中括弧,json返回**{}**和

    PHP json返回大括弧和中括弧,json返回{}和[] 有时候,前端要求返回 大括弧和中括弧 前端要求返回{...

  • 字符串中是否括弧是成对合法出现

    需求: 有一串字符串,里面包含若干个括弧,如何判断这些括弧是以成对出现的 (),且是合法的括弧 (),括弧支持嵌套...

  • 双括弧

    双括弧 如果大洪水 又一次来了 并且没时间 造一艘 诺亚方舟 只有一个 双括弧 可以度过这场灾难 双括弧里...

  • 随笔

    生命其實就是一個過程,不是一個結果。 生命是一個括弧。左邊括弧是出生,右邊括弧是死亡,我們要做的事情就是填括弧,要...

  • 括弧子部

    本文参加‘青春’大赛,本人保证本文为本人原创,如有问题则与主办方无关,自愿放弃评优评奖资格。 深圳北理莫斯科大学 ...

  • Java HashMap 优雅方式初始化赋值

    第一层括弧实际是定义了一个匿名内部类 (Anonymous Inner Class),第二层括弧实际上是一个实例初...

  • 括弧匹配检验

    括弧匹配检验 运行时限: 10000 ms 单次运行时限: 10000 ms 内存限制: 32 MB总提交...

  • 判断括弧匹配

  • 知识点补充2.1—行列式和矩阵

    行列式 矩阵 矩阵为圆括弧,印刷方便用中括弧。矩阵的转置设A为m×n阶矩阵[https://baike.baidu...

网友评论

      本文标题:画个括弧

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