我正处于制作简单图表的早期阶段。但是,我在以编程方式绘制形状方面遇到了一些麻烦。
我尝试了很多这样的变体:
let donutChart = CounterView(frame = CGRectMake(100, 100, 100, 100))
cellView.addSubview(donutChart)
没有错误,但在过去的两天里,我已经能够在屏幕上绘制任何内容了。
我尝试绘制的课程是:
import UIKit
let NoOfGlasses = 8
let π:CGFloat = CGFloat(M_PI)
class CounterView: UIView {
var counter: Int = 5 {
didSet {
if counter <= NoOfGlasses {
//the view needs to be refreshed
setNeedsDisplay()
}
}
}
var outlineColor: UIColor = UIColor.blueColor()
var counterColor: UIColor = UIColor.orangeColor()
override func drawRect(rect: CGRect) {
// 1
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
// 2
let radius: CGFloat = max(bounds.width, bounds.height)
// 3
let arcWidth: CGFloat = 76
// 4
let startAngle: CGFloat = 3 * π / 4
let endAngle: CGFloat = π / 4
// 5
var path = UIBezierPath(arcCenter: center,
radius: bounds.width/2 - arcWidth/2,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
// 6
path.lineWidth = arcWidth
counterColor.setStroke()
path.stroke()
}
}
这应该导致这种基本形状:
有关如何以编程方式在视图控制器中绘制此形状的任何帮助将非常感谢!
答案 0 :(得分:1)
我把你的课程放在了游乐场。我能够使用以下命令绘制它:
let myview = CounterView()
myview.frame = CGRectMake(100,100,300,300)
myview.counter = 5
这是它的样子:
答案 1 :(得分:0)
您是否忘记在屏幕上添加某个视图子视图的CounterView?