How can I calculate the center of an NSButton?
I'm not sure how to find the center of my button:
spinner = [[EKActivityIndicatorView alloc] initWithFrame:CGRectMake([followButton bounds].origin.x/2, [followButton bounds].ori开发者_开发问答gin.y/2, 16, 16)];
Dividing the frame.x and frame.y does not do the trick
The correct way is to do:
CGRectMake(CGRectGetMidX([followButton frame]), CGRectGetMidY([followButton frame]), 16, 16)
Alternatively, the correct formula is:
CGRect frame;
CGPoint center;
frame = [view frame];
center.x = frame.origin.x + (frame.size.width / 2);
center.y = frame.origin.y + (frame.size.height / 2);
What you want to do is:
CGRectMake(NSMidX([followButton frame], NSMidY([followButton frame]), 16, 16)
Using the bounds will return (0,0) for the origin, only taking into account the size of the button. Using the frame will provide the origin as well.
Edit: sorry, my code is a bastardized cocoa/core graphics hybrid.
精彩评论