0
votes

I am new to using CGPoints to move objects around in Swift. I was wondering if you had a custom view that was of width and height 320, how you would be able to figure out the constraints of that in a CG coordinate system.

For example I have a ball object. If I set

ball.center.y = CGFloat(390) //where 390 is some random point

Then the center of y is a little above the half point line in the view. I know the top left is 0,0 for CGPoint and was wondering if there was a standard coordinate system when dealing with two dimensional coordinates.

1
ball.center and ball.frame.origin itself are CGPoints - Navneet Gill
Can you give more details as to what your exact problem is? What have you tried that hasn't worked? - Ashley Mills
I want to move a ball based on the angle of tilt of the phone, and center it at a 45 degree center tilt. - timedwalnut
@AshleyMills so when the phone is tilted 45 degrees, the ball should be in the center of the screen. - timedwalnut
That sounds like an entirely different question to the one you asked. Your question is something more like "How do I translate device position in 3D space into 2D coordinates?" - Ashley Mills

1 Answers

2
votes

It seems like you're looking for this Apple documentation… https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/CoordinateSystem.html

The default coordinate system has its origin at the upper left of the drawing area, and positive values extend down and to the right from it.

If you want to position the centre of your view at the centre of its superview…

let superview = view.superview!
view.center = CGPoint(x: superview.bounds.midX, y: superview.bounds.midY)