0

I am trying to create a circle and spin it about its center (see my question about drawing this)

So in my ViewController I have a func that creates the wheel inside a UIView I have drawn, myWheel, which is put in the center of the View using constraints. This is called in viweDidLoad()

Inside that func I do a print of some values to check

myWheel.center (270,270) 
myWheel.frame (0,0,540,540)
myWheel.bounds (0,0,540,540) 

originally I was trying to use the .center value to draw the wheel at the center of myWheel view, but when I press my button which calls the function to spin the wheel around its center it gives different values. In the function attached to my @IBAction which adds the rotation:

myWheel.center (161.5,161.5)
myWheel.frame (8,8,307,307) 
myWheel.bounds (0,0,307,307)

I suspect the values during creation are before the screen size is known - but can't find any doco which talks about this. The issue is how do I determine during creation the center point which I need to draw my circle?

Community
  • 1
  • 1
Dan
  • 1,343
  • 2
  • 9
  • 12

1 Answers1

1

During viewDidLoad, the view is just loaded and its bounds wont be still finalized, you should check for the final bounds in either viewWillappear, viewDidAppear or viewWillLayoutSubViews method.

But in you're case, I believe the myWheel view that you have added as subview has its constraints setup so that the view's bounds varies along with the parent's. For eg Setting Trailing space, Leading space w.r.t parent.

If you want the circle to be drawn at the center, you can set the center x, y constraints along with desired height, width constraints for the myWheel view.

Adithya
  • 4,545
  • 3
  • 25
  • 28
  • So I used viewDidAppear and now I get the same result in both places, thank you Only problem now is the CABasicAnimation I add seems to have a slightly different anchor point! – Dan Mar 04 '16 at 22:47