开发者

Getting the center point of an NSView

I need to get the center point of a NSView in the form of a CGPoint, how can I achieve开发者_JAVA百科 this? I am basically looking for the equivalent of the UIKit, UIView.center


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜