UIBezierPath 未按预期更新
UIBezierPath not updated as expected
这是我的自定义视图代码:
class CircleView3: UIView {
let endPoint = 270.0
var index = 0.0
var startPoint: Double {
get{
index++
let div = 360.0/60.0
let vaule = div * index
return 290.0 + vaule
}
}
func degreesToRadians (number: Double) -> CGFloat {
return CGFloat(number) * CGFloat(M_PI) / 180.0
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let startAngleRadiant: CGFloat = degreesToRadians(startPoint)
let endAngleRadiant: CGFloat = degreesToRadians(endPoint)
print("start = \(startAngleRadiant)")
print("end = \(endAngleRadiant)")
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let path = UIBezierPath()
path.moveToPoint(center)
path.addArcWithCenter(center, radius: self.frame.height/2.2, startAngle: startAngleRadiant, endAngle: endAngleRadiant, clockwise: true)
path.addLineToPoint(center)
let strokeColor: UIColor = UIColor(red: 155.0/255.0, green: 137.0/255.0, blue: 22.0/255.0, alpha: 1.0)
strokeColor.setFill() //strokeColor.setStroke()
path.fill()
path.stroke()
}
}
我正在画一个圆圈。我每秒从视图控制器调用 setNeedsDisplay
函数。我应该画一个每秒减少其起点和终点的圆圈,但即使在更新后圆圈仍保持其最后一行(位置)。再加上圆圈没有填充我的颜色,只是填充了线条。
http://www.mediafire.com/watch/xnkew5eu8da5wub/IMG_0066.MOV
更新
我在drawRect
中打印起始值,并且这些值是正确的:
start = 293.6
start = 297.2
start = 300.8
start = 304.4
start = 308.0
start = 311.6
start = 315.2
start = 318.8
start = 322.4
start = 326.0
start = 329.6
start = 333.2
start = 336.8
start = 340.4
当然,这些是在将它们更改为弧度之前的值。
更新 2
调用strokeColor.setFill()
后情况好多了,但还是有问题。请观看这个新视频:
为了它的价值,我将您的代码放入一个项目中,我看到一个缩小的圆圈,因为在 customView 上调用了 setNeedsDisplay
。它画得不像你的视频。
这是我的附加代码:
class ViewController: UIViewController {
@IBOutlet weak var circleView: CircleView3!
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: "updateCircle", userInfo: nil, repeats: true)
}
func updateCircle() {
circleView.setNeedsDisplay()
}
}
这是我的自定义视图代码:
class CircleView3: UIView {
let endPoint = 270.0
var index = 0.0
var startPoint: Double {
get{
index++
let div = 360.0/60.0
let vaule = div * index
return 290.0 + vaule
}
}
func degreesToRadians (number: Double) -> CGFloat {
return CGFloat(number) * CGFloat(M_PI) / 180.0
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let startAngleRadiant: CGFloat = degreesToRadians(startPoint)
let endAngleRadiant: CGFloat = degreesToRadians(endPoint)
print("start = \(startAngleRadiant)")
print("end = \(endAngleRadiant)")
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let path = UIBezierPath()
path.moveToPoint(center)
path.addArcWithCenter(center, radius: self.frame.height/2.2, startAngle: startAngleRadiant, endAngle: endAngleRadiant, clockwise: true)
path.addLineToPoint(center)
let strokeColor: UIColor = UIColor(red: 155.0/255.0, green: 137.0/255.0, blue: 22.0/255.0, alpha: 1.0)
strokeColor.setFill() //strokeColor.setStroke()
path.fill()
path.stroke()
}
}
我正在画一个圆圈。我每秒从视图控制器调用 setNeedsDisplay
函数。我应该画一个每秒减少其起点和终点的圆圈,但即使在更新后圆圈仍保持其最后一行(位置)。再加上圆圈没有填充我的颜色,只是填充了线条。
http://www.mediafire.com/watch/xnkew5eu8da5wub/IMG_0066.MOV
更新
我在drawRect
中打印起始值,并且这些值是正确的:
start = 293.6
start = 297.2
start = 300.8
start = 304.4
start = 308.0
start = 311.6
start = 315.2
start = 318.8
start = 322.4
start = 326.0
start = 329.6
start = 333.2
start = 336.8
start = 340.4
当然,这些是在将它们更改为弧度之前的值。
更新 2
调用strokeColor.setFill()
后情况好多了,但还是有问题。请观看这个新视频:
为了它的价值,我将您的代码放入一个项目中,我看到一个缩小的圆圈,因为在 customView 上调用了 setNeedsDisplay
。它画得不像你的视频。
这是我的附加代码:
class ViewController: UIViewController {
@IBOutlet weak var circleView: CircleView3!
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: "updateCircle", userInfo: nil, repeats: true)
}
func updateCircle() {
circleView.setNeedsDisplay()
}
}