如何在swift 3中绘制圆弧

2024-01-02

我有一些来自网络服务的百分比值。根据百分比值我想在a中画一些圆弧UIView通过以下方式。

白色圆圈是一个UIView我尝试通过这种方式来实现这一目标。

func colorProgress()
{
    let circleColorPath=UIBezierPath(arcCenter: CGPoint.init(x: RDcircleEnum.circleCenterX.getValues(), y: RDcircleEnum.circleCenterY.getValues()), radius: self.innerCircleRadius, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*self.progressAmount), clockwise: true)



    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circleColorPath.cgPath

    //change the fill color
    shapeLayer.fillColor = UIColor.blue.cgColor
    //you can change the stroke color
    shapeLayer.strokeColor = UIColor.red.cgColor
    //you can change the line width
    shapeLayer.lineWidth = 3.0
    self.progressColor.setFill()
    circleColorPath.fill()
}

但这并没有给出我想要的。我想根据百分比值绘制不同长度的圆弧。请帮我。


这是您想要实现的完美逻辑:

https://codereview.stackexchange.com/questions/97615/draw-an-arc-based-on-a-given-percentage https://codereview.stackexchange.com/questions/97615/draw-an-arc-based-on-a-given-percentage

自定义 UIView 类

import UIKit

class DemoView: UIView {

var startPoint: CGFloat = 0
var color: UIColor = UIColor.yellow
var trackColor: UIColor = UIColor.gray
var trackWidth: CGFloat = 1
var fillPercentage: CGFloat = 100

override init(frame: CGRect) {

    super.init(frame: frame)
    self.backgroundColor = UIColor.clear

} // init

required init(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)!
    self.backgroundColor = UIColor.clear

} // init

private func getGraphStartAndEndPointsInRadians() -> (graphStartingPoint: CGFloat, graphEndingPoint: CGFloat) {

    // make sure our starting point is at least 0 and less than 100
    if ( 0 > self.startPoint ) {
        self.startPoint = 0
    } else if ( 100 < self.startPoint ) {
        self.startPoint = 100
    } // if

    // make sure our fill percentage is at least 0 and less than 100
    if ( 0 > self.fillPercentage ) {
        self.fillPercentage = 0
    } else if ( 100 < self.fillPercentage ) {
        self.fillPercentage = 100
    } // if

    // we take 25% off the starting point, so that a zero starting point
    // begins at the top of the circle instead of the right side...
    self.startPoint = self.startPoint - 25

    // we calculate a true fill percentage as we need to account
    // for the potential difference in starting points
    let trueFillPercentage = self.fillPercentage + self.startPoint

    let π: CGFloat = .pi

    // now we can calculate our start and end points in radians
    let startPoint: CGFloat = ((2 * π) / 100) * (CGFloat(self.startPoint))
    let endPoint: CGFloat = ((2 * π) / 100) * (CGFloat(trueFillPercentage))

    return(startPoint, endPoint)

} // func

override func draw(_ rect: CGRect) {

    // first we want to find the centerpoint and the radius of our rect

    let center: CGPoint = CGPoint(x: rect.midX, y: rect.midY)
    let radius: CGFloat = rect.width / 2

    // make sure our track width is at least 1
    if ( 1 > self.trackWidth) {
        self.trackWidth = 1
    } // if

    // and our track width cannot be greater than the radius of our circle
    if ( radius < self.trackWidth ) {
        self.trackWidth = radius
    } // if

    // we need our graph starting and ending points
    let (graphStartingPoint, graphEndingPoint) = self.getGraphStartAndEndPointsInRadians()

    // now we need to first draw the track...
    let trackPath = UIBezierPath(arcCenter: center, radius: radius - (trackWidth / 2), startAngle: graphStartingPoint, endAngle: 2.0 * .pi, clockwise: true)
    trackPath.lineWidth = trackWidth
    self.trackColor.setStroke()
    trackPath.stroke()

    // now we can draw the progress arc
    let percentagePath = UIBezierPath(arcCenter: center, radius: radius - (trackWidth / 2), startAngle: graphStartingPoint, endAngle: graphEndingPoint, clockwise: true)
    percentagePath.lineWidth = trackWidth
    percentagePath.lineCapStyle = .round
    self.color.setStroke()
    percentagePath.stroke()

    return

} // func } // class

viewController中的实现:

 let demoView : DemoView = DemoView()
 demoView.frame = CGRect(x: 50, y: 100, width: 200, height: 200)
 demoView.trackWidth = 5
 demoView.startPoint = 0
 demoView.fillPercentage = 25
 self.view.addSubview(demoView)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在swift 3中绘制圆弧 的相关文章

  • 访问目标 c 中的类方法。使用 self 还是类名?

    我正在学习 iOS 编程 并且对以下有关关键字 self 的使用的代码感到困惑 据我了解 self就像Java的this 它指的是当前实例 当我想调用类方法时 通常的方式应该是这样 PlayingCard validSuits 但是侵入实例
  • 在 Swift 中从 Parse 加载图像

    我成功地将数据从 Parse 提取到 swift 中 但我的图像似乎没有按照我的方式工作 在我的 cellForRowAtIndexPath 方法中 我执行以下操作 var event AnyObject eventContainerArr
  • 更改 iOS7 中 UIAlertView 的字体大小

    我想更改alertView中消息文本和标题文本的字体大小 苹果网站上没有任何文档谈到这一点 但苹果在其子类注释中表示 UIAlertView 类旨在按原样使用 请参考以下链接 https developer apple com librar
  • 更改 NSMutableAttributedString 中链接的颜色

    我有以下代码 但我的链接始终是蓝色的 我如何改变它们的颜色 string addAttribute NSLinkAttributeName value tag range NSMakeRange position length string
  • 在 Swift 中有条件导入基于 iOS 版本的框架(如 Speech)?

    有没有办法有条件地导入框架Swift基于运行时 iOS 版本 特别是 我有一个部署目标为的应用程序iOS 8 我想使用新的Apple Speech应用程序中的框架 如果在运行时可用 我知道 available iOS 10 代码部分的指令
  • iOS - UIBarButtonItem - 后退按钮标题垂直位置不调整

    我正在设置自定义字体UIBarButtonItem对象使用UIAppearance 这工作正常并正确设置字体 但是 我确实需要调整按钮标题的垂直位置以适应新字体的大小 UIBarButtonItem appearance setTitleT
  • React Native Detox:无法接受系统警报弹出的位置权限

    使用以下命令从命令行启动应用程序detox test configuration ios sim debug 通过代码启动应用程序await device launchApp permissions location always noti
  • 在phonegap中播放本地声音

    我有一个 wav文件在我的www文件夹 我正在使用 jQuery 和以下代码 警报响起 但声音不播放 难道我做错了什么
  • ios如何搜索目录

    我想知道如何检查我的应用程序中是否存在目录 例如 如果我想搜索我的应用程序文档中是否存在文件夹 以及如何在其中创建新文件夹 此致 检查文件是否存在 BOOL fileExistsAtAbsolutePath NSString filenam
  • Alamofire 发布请求问题

    let parameters checkout email email protected cdn cgi l email protection line items variant id 31342168513 quantity 1 sh
  • HttpClient setReachabilityStatusChangedBlock 声明没有接口

    尝试使用 AFNetworkings ReachabilityStatusChanged 但得到 HTTPCLIENT 没有可见的 interface 声明选择器 setReachabilityStatusChangeBlock 但Http
  • 强制本地化图像或图像资产

    正如在这个问题中 如何强制 NSLocalizedString 使用特定语言 https stackoverflow com questions 1669645 how to force nslocalizedstring to use a
  • Objective c RSA 与 OAEP 填充 sha256 之前的 ios 10

    我正在研究一种在iPhone中使用RSA加密方法的加密方法 到目前为止我可以用这种方法实现获取加密字符串 该字符串被服务器成功解密 SecKeyRef keyRef self addPublicKey pubKey SecKeyAlgori
  • 如何获得 UICollectionView 的矩形

    我想在 UICollectionView 中找到节标题的框架 我对 UITableView 也有类似的情况 为此 我能够通过执行以下操作来获得其正确性 CGRect rect self tableView rectForHeaderInSe
  • iOS 中第一响应者的正式定义是什么?

    据我所知 第一响应者对象是根据输入活动等接收回调信号 并且它将沿着链向上冒泡 直到找到愿意处理它的响应者 但更正式地说 第一响应者的范围是什么 例如 它是应用程序范围的响应程序吗 似乎作为第一响应者只是说这个特定的对象将收到交互通知 其他响
  • XOR或其他简单的文件混淆方法ios目标c

    我正在开发一个使用受版权保护的 pdf 文件的 ios 应用程序 我正在寻找一些简单的方法来混淆文件以确保安全 这些方法不需要我经历额外的大众市场 CCATS 加密审查流程 但也将确保版权所有者已尽我所能来保证他们的数据安全 当然 我用密码
  • 从应用程序打开/关闭 iPhone 的 Wifi

    我需要打开或关闭 iPhone 中的 wifi 有什么方法可以做到吗 还是被禁止了 私有框架 所有使用公共 SDK 编写的应用程序都经过沙箱处理 他们只能访问苹果认为可以在该沙箱中使用的属性和数据 恐怕 Wi fi 不在列表中 请看一下这个
  • 如何在复杂的层次结构中上下同步 CALayer 和 UIView 动画

    See 如何在整个层次结构中管理 CALayer 动画 https stackoverflow com questions 26917908 how to manage calayer animations throughout a hie
  • FIRApp 链接器错误 [“_OBJC_CLASS_$_FIRApp”]

    我已经搜索过 SO 和 Google 但找不到有效的答案 我已经在多个项目中使用了新的 Firebase Cocoapod 但是现在 当将其添加到不同的项目时 我收到以下错误 我正在使用 Xcode 7 3 1 和 cocoapods 1
  • 如何在 Swift 中将所有 iOS 设备的标签水平居中

    我不知道如何使标签在图像视图中水平居中 标签说 You ve been here What would you rate us 我想要What would you rate us属于 You ve been here 我试图完成此操作的方法

随机推荐