UIBezierPath未按预期更新

时间:2015-11-22 14:35:49

标签: ios swift drawrect uibezierpath

这是我自定义视图的代码:

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()后情况好一些,但我仍然看到了问题。请看这个新视频:

http://www.mediafire.com/watch/nb1b3v2zgqletwb/IMG_0069.MOV

1 个答案:

答案 0 :(得分:3)

为了它的价值,我把你的代码放在一个项目中,我看到一个缩小的圈子,因为setNeedsDisplay在customView上被调用。它不像您的视频那样绘制。

这是我的附加代码:

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