Getting the center point of an NSView

情到浓时终转凉″ 提交于 2019-12-05 00:07:48

This code will get you that CGPoint:

CGPointMake((myView.frame.origin.x + (myView.frame.size.width / 2)),
            (myView.frame.origin.y + (myView.frame.size.height / 2)))
lms

A simple and easy to read way is:

CGPointMake(NSMidX(myView.frame), NSMidY(myView.frame))

I would suggest making this code, a category on NSView with a read only property named center.

To get an NSPoint of the center, you can just get the x-coordinate of the origin and add (width/2), and get the y-coordinate of the origin and add (height/2). Then you can convert it into a CGPoint.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!