2011-04-05 36 views
0

我不知道如何找到我的按鈕的中心:如何計算NSButton的中心?

spinner = [[EKActivityIndicatorView alloc] initWithFrame:CGRectMake([followButton bounds].origin.x/2, [followButton bounds].origin.y/2, 16, 16)]; 

除以frame.x和frame.y不會做的伎倆

回答

2

正確的方法是要做到:

CGRectMake(CGRectGetMidX([followButton frame]), CGRectGetMidY([followButton frame]), 16, 16) 

可替換地,正確的公式爲:

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

你想要做的是:

CGRectMake(NSMidX([followButton frame], NSMidY([followButton frame]), 16, 16) 

使用邊界將返回(0,0)原點,只考慮按鈕的大小。使用框架也會提供原點。

編輯:對不起,我的代碼是一個混蛋可可/核心圖形混合。