UIBezierPath not updated as expected

2019-07-17 05:32发布

This is my code for my custom view:

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

I am drawing a circle. I call the setNeedsDisplay function from a view controller each second. I am supposed to draw a circle that is reducing its start and end point each second, but the circle keep its last line (position) even after the update. Plus the circle is not filled with my color, just the line is filled.

http://www.mediafire.com/watch/xnkew5eu8da5wub/IMG_0066.MOV

Update

I print the start values in drawRect, and the values are correct:

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

of course these are the values before changing them to radians.

Update 2

After I call the strokeColor.setFill() the situation is better, but I still see the problem. Please see this new video:

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

1条回答
趁早两清
2楼-- · 2019-07-17 05:59

For what its worth, I put your code in a project and I see a shrinking circle as setNeedsDisplay is called on the customView. It does not draw like your video.

Here is my additional code:

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()
    }
}
查看更多
登录 后发表回答