2

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

vacawama
  • 150,663
  • 30
  • 266
  • 294
sarah
  • 1,201
  • 1
  • 9
  • 29
  • You need to say strokeColor.setFill() before path.fill() to have it fill. You should post the code of the timing function where you're calling setNeedsDisplay – beyowulf Nov 22 '15 at 14:53
  • @beyowulf okay the set fill solved the fill problem, but i still in my main problem, why do you need the code that calls the setneed display? that code does nothing except calling the setneedsdisplay for the view. whick will call my draw rect function inside the CircleView3 – sarah Nov 22 '15 at 14:56
  • It seems like the path is getting drawn on top of itself over and over again. Are you doing anything with the view's layer? Or creating the view and adding it to your view controller's view in the timer function? – beyowulf Nov 22 '15 at 15:13
  • @beyowulf no no i don't do anything, after setting the set fill on the colore, the situation imporoved a little bit, i am try to upload a new video, – sarah Nov 22 '15 at 15:18
  • @beyowulf i add a new video, you look for me? – sarah Nov 22 '15 at 15:22
  • 1
    Have you looked at @vacawama's answer? I did the same thing he did and got the same result. Are you saying something like setNeedsDisplayInRect? – beyowulf Nov 22 '15 at 15:32
  • @beyowulf so it works with you? my code? i don't call set needs display in my view, i called it from outside (inside the view constroller). please help – sarah Nov 22 '15 at 15:37
  • I used the code @vacawama posted he's calling circleView.setNeedsDisplay() from the view controller that has the circle view as a subview. – beyowulf Nov 22 '15 at 15:40
  • @sarah Do you really want it updated periodically, or do you want a smooth, continuous animation. If so, you can use `CABasicAnimation` to [animate the drawing of a circle](http://stackoverflow.com/a/32898165/1271826), and it's much easier than the code above. FYI, if you stay with the above code, I'd advise (a) don't set the angle from a calculation stemming from `index++`, but rather use timestamps; and (b) consider using `CADisplayLink` rather than `NSTimer`. [This answer](http://stackoverflow.com/a/14962473/1271826) is for a different animation, but illustrates the concept. – Rob Nov 22 '15 at 18:22
  • @Rob hello, i didn't know about any of that CABasicAnimation. and CADisplayLink, I will study the links you said, and try to understand. thank you always very much – sarah Nov 22 '15 at 21:01

1 Answers1

3

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()
    }
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • that is i dont understand. do work with u? i in my view controller have this formatter.dateFormat = "mm:ss" let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true) timer.fire() and the function update is : circleTimerView.setNeedsDisplay() where circleTimerView is the outlet of the view. u understand me? i put a new video – sarah Nov 22 '15 at 15:24
  • I'm using your exact `CircleView3` code from above and my code as shown and your view draws correctly. There must be something else different about your setup. Try creating a new project using just my code and your `CircleView3` and you should see it works properly. – vacawama Nov 22 '15 at 15:37
  • i am trying to see the prboem, first i don't knwo why this black backgrond is happen. second, really i dont do anything anything else in my code, i just all the set needs display. – sarah Nov 22 '15 at 15:39
  • What version of Xcode are you using? I'm setting the background color of my custom view in Interface Builder. Black or white, the circle looks fine. – vacawama Nov 22 '15 at 15:41
  • i am using Version 7.1 (7B91b) . i dont know what to say, i still have the problem, i hope someone help me or you please help me – sarah Nov 22 '15 at 15:43
  • i write code that works with people's machine not my machine? ? how that possible – sarah Nov 22 '15 at 15:44
  • I'm using version 7.1.1. I highly doubt that makes a difference, but you might want to update to be sure. Have you tried creating a new project with just my code? Copy the `CircleView3` code from this question. It would take all of 5 minutes. – vacawama Nov 22 '15 at 15:47
  • i found the soulition , i did this circleTimerView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(1.0) on my view controler, i changed the background to while. and that works good, i am confused though the problem solved, but i am confused about the reason. – sarah Nov 22 '15 at 15:50
  • I'm glad it's working. I don't know why it was failing for your in the first place. `circleTimerView.backgroundColor = .whiteColor()` should be sufficient though. `UIColor` is inferred by Swift, and the default alpha is `1.0`. – vacawama Nov 22 '15 at 15:58