RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1111139
Accepted
JiosDev
JiosDev
Asked:2020-04-16 18:15:44 +0000 UTC2020-04-16 18:15:44 +0000 UTC 2020-04-16 18:15:44 +0000 UTC

更改 bezierPath 的半径

  • 772

所有视图尺寸的宽度仍然相同。我们希望弧线改变它们的大小,就好像它们被赋予了约束一样。

 @IBDesignable class ArcView: UIView {


    typealias ArcAction = () -> Void

    struct ArcInfo {
        var outlinePath: UIBezierPath
        var action: ArcAction
    }

    private var arcInfos: [ArcInfo]!

    let bgShapeLayer = CAShapeLayer()

    var redRadius:   Float?
    var greenRadius: Float?


private var pathLineWidth: CGFloat {
    return frame.size.width / 15.0
    }
    private var normalArcRadius: CGFloat {
     return   frame.size.width / 2 - pathLineWidth * 1.5
    }

    private var tappedArcRadius: CGFloat {
      return  frame.size.width / 2 - pathLineWidth / 2
    }


    required init?(coder: NSCoder) {
        super.init(coder: coder)
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(tap(_ :)))
        addGestureRecognizer(recognizer)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        }


    override func draw(_ rect: CGRect) {

        let fullCircle = CGFloat.pi * 2
        let arcAngle = fullCircle * 1.5 / 6

        var lastArcAngle = CGFloat.pi / 4.0 + CGFloat.pi //-CGFloat.pi
        // background
        let backPath = UIBezierPath(arcCenter: CGPoint(x: rect.width/2, y: rect.height/2), radius: 55.0, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
        #colorLiteral(red: 0.9931474328, green: 0.9932896495, blue: 0.9931163192, alpha: 1).setStroke()
        backPath.lineWidth = 2.3
        backPath.stroke()

        #colorLiteral(red: 0.9212146401, green: 0.9490351081, blue: 0.9671724439, alpha: 1).setFill()
        backPath.fill()

        arcInfos = []

        // Red Arc
        func redArc( action: @escaping ArcAction) {

            let path = UIBezierPath(arcCenter: CGPoint(x: rect.width/2, y: rect.height/2), radius: 46, startAngle: lastArcAngle, endAngle: lastArcAngle + arcAngle, clockwise: true)

            #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1).setStroke()
            path.lineWidth = 10
            path.stroke()
            lastArcAngle += arcAngle

            // separators
            #colorLiteral(red: 0.927436769, green: 0.9490308166, blue: 0.967099607, alpha: 1).setStroke()
            let outlinePath = hitTestPath(for: path)
            outlinePath.lineWidth = 3
            outlinePath.stroke()

            arcInfos.append(ArcInfo(outlinePath: outlinePath, action: action))
        }

        //Green Arc
        func greenArc( action: @escaping ArcAction) {

            let path = UIBezierPath(arcCenter: CGPoint(x: rect.width/2, y: rect.height/2), radius: CGFloat(greenRadius ?? 56), startAngle: lastArcAngle, endAngle: lastArcAngle + arcAngle, clockwise: true)

            #colorLiteral(red: 0.2443430722, green: 0.800511539, blue: 0.4006313086, alpha: 1).setStroke()
            path.lineWidth = 10
            path.stroke()
            lastArcAngle += arcAngle

            // separators
            #colorLiteral(red: 0.927436769, green: 0.9490308166, blue: 0.967099607, alpha: 1).setStroke()
            let outlinePath = hitTestPath(for: path)
            outlinePath.lineWidth = 3
            outlinePath.stroke()

            arcInfos.append(ArcInfo(outlinePath: outlinePath, action: action))
        }

        //Add Arc
        greenArc {
            self.redRadius = 46
            self.greenRadius = 56

        }

        redArc {
            self.redRadius = 56
            self.greenRadius = 46

        }


    }

    @objc func tap(_ recognizer: UITapGestureRecognizer) {
        let location = recognizer.location(in: self)

        if let hitPath = (arcInfos.first { $0.outlinePath.contains(location) }) {
            hitPath.action()
            setNeedsDisplay()
            //print(hitPath)
        }
    }

    func hitTestPath(for path: UIBezierPath) -> UIBezierPath {
        let pathCopy = path.cgPath.copy(strokingWithWidth: 15, lineCap: .butt, lineJoin: .miter, miterLimit: 0)
        return UIBezierPath(cgPath: pathCopy)
    }
}

protocol circleViewDelegate: NSObjectProtocol {
    func selectedType(_ type: typeCircle)
}
ios
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    schmidt9
    2020-04-21T22:52:57Z2020-04-21T22:52:57Z

    我以之前的答案为基础,重写了一遍,结果如下

    class ArcsView: UIView {
    
        typealias ArcAction = () -> Void
    
        class ArcInfo {
    
            var normalPath: UIBezierPath
    
            var tappedPath: UIBezierPath
    
            var currentPath: UIBezierPath {
                isTapped ? tappedPath : normalPath
            }
    
            var outlinePath: UIBezierPath {
                hitTestPath(for: isTapped ? tappedPath : normalPath)
            }
    
            var strokeColor: UIColor
    
            var action: ArcAction
    
            var isTapped = false
    
            init(normalPath: UIBezierPath, tappedPath: UIBezierPath, strokeColor: UIColor, action: @escaping ArcAction) {
                self.normalPath = normalPath
                self.tappedPath = tappedPath
                self.strokeColor = strokeColor
                self.action = action
            }
    
            private func hitTestPath(for path: UIBezierPath) -> UIBezierPath {
                let pathCopy = path.cgPath.copy(strokingWithWidth: 20, lineCap: .butt, lineJoin: .miter, miterLimit: 0)
                return UIBezierPath(cgPath: pathCopy)
            }
        }
    
        private var pathLineWidth: CGFloat {
            frame.size.width / 15.0
        }
    
        private var normalArcRadius: CGFloat {
            frame.size.width / 2 - pathLineWidth * 1.5
        }
    
        private var tappedArcRadius: CGFloat {
            frame.size.width / 2 - pathLineWidth / 2
        }
    
        private var arcInfos: [ArcInfo]!
    
        required init?(coder: NSCoder) {
            super.init(coder: coder)
    
            let recognizer = UITapGestureRecognizer(target: self, action: #selector(tap(_ :)))
            addGestureRecognizer(recognizer)
    
            initArcs()
        }
    
        override func draw(_ rect: CGRect) {
            arcInfos.forEach { info in
                info.strokeColor.set()
                info.currentPath.stroke()
            }
        }
    
        private func initArcs() {
            arcInfos = []
    
            var lastArcAngle = -CGFloat.pi
    
            func addArc(color: UIColor, percentage: CGFloat, index: Int, action: @escaping ArcAction) {
    
                let fullCircle = CGFloat.pi * 2
                let arcAngle = fullCircle * percentage
    
                let normalPath = UIBezierPath(arcCenter: CGPoint(x: frame.width / 2, y: frame.height / 2), radius: normalArcRadius, startAngle: lastArcAngle, endAngle: lastArcAngle + arcAngle, clockwise: true)
                normalPath.lineWidth = pathLineWidth
    
                let tappedPath = UIBezierPath(arcCenter: CGPoint(x: frame.width / 2, y: frame.height / 2), radius: tappedArcRadius, startAngle: lastArcAngle, endAngle: lastArcAngle + arcAngle, clockwise: true)
                tappedPath.lineWidth = pathLineWidth
    
                let arcInfo = ArcInfo(normalPath: normalPath,
                                      tappedPath: tappedPath,
                                      strokeColor: color,
                                      action: action)
    
                arcInfos.append(arcInfo)
    
                lastArcAngle += arcAngle
            }
    
            addArc(color: .red, percentage: 1.5 / 6.0, index: 1) {
                print("action 1")
            }
    
            addArc(color: .green, percentage: 1.5 / 6.0, index: 2) {
                print("action 2")
            }
    
            addArc(color: .blue, percentage: 1.5 / 6.0, index: 3) {
                print("action 3")
            }
    
            addArc(color: .purple, percentage: 1.5 / 6.0,index: 4) {
                print("action 4")
            }
        }
    
        @objc func tap(_ recognizer: UITapGestureRecognizer) {
            let location = recognizer.location(in: self)
    
            if let hitPath = (arcInfos.first { $0.outlinePath.contains(location) }) {
                arcInfos.forEach { $0.isTapped = ($0 === hitPath) }
    
                hitPath.action()
    
                setNeedsDisplay()
            }
        }
    
    }
    
    • 1

相关问题

  • UIImageView 相对于 superview 缩放后的位置

  • 在密码中授权 type basic 时,符号#swift 4 被错误传输到服务器

  • 将 AppDelegate.h 添加到项目中

  • 调用共享表时出错

  • 接近传感器模拟

  • 帮助 ios 应用程序架构 (MVP)

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5