2012-05-31 47 views
1

我有一個視圖(實際上,UIControl)作爲我的UIViewController的一部分。這個視圖是在IB中設計的,然後是基於某些用戶操作的代碼,我正在改變視圖的框架。整個事情發生在CATransition內部。代碼如下所示:如何查找視圖的大小/位置?

CATransition *anim = [CATransition animation]; 
anim.type = kCATransitionFade; 
anim.duration = 0.5; 
anim.delegate = self; 
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

[mainPanel.layer addAnimation:anim forKey:@"changeTextTransition"]; 

float totalHeight = ...; // this is the Y of the frame 
float max_width = ...; // this is the width of the frame 
float bh = ...;   // this is the height of the frame 

... //some computations to determine the frame for my view 

[blockView setFrame:CGRectMake(0, totalHeight, max_width, bh)]; 

//This is for debugging 
CGSize sg = answer1.frame.size; 
NSLog(@"width: %f, height: %f", sg.width, sg.height); 
//This outputs: width: 260.000000, height: 29.000000 

到目前爲止,一切都很好,並在NSLog的我看到的寬度和高度正確的價值觀。然而,當顯示視圖時,其尺寸已經改變。然後,我添加了相同的調試animationDidStop - 和看到不同的結果:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
    [scroll flashScrollIndicators]; 
    CGSize sg = answer1.frame.size; 
    NSLog(@"width: %f, height: %f", sg.width, sg.height); 
    //This outputs: width: 280.000000, height: 49.000000 
} 

注意,不知何故某處視圖的大小發生了變化。我無法理解在哪裏/爲什麼/如何發生這種情況。任何想法在哪裏看?

回答

0

爲了找到視圖時調整大小,你可以子類的UIView和重寫以下

- (void)setFrame:(CGRect)frame 
{ 
    [super setFrame:frame]; 
} 

現在這個功能將在每次視圖改變其大小

+0

這就是做這件事的一種方法被調用,但似乎有點太多的工作。子類化UIControl,更改我的代碼並更改NIB,全部只是爲了找到誰/何時調用其中一種方法。如果我無法以其他方式解決問題,我可能必須這樣做。 –

+0

希望你找到一個更簡單的方式,但這是我知道的更快的方式 –

+0

是的,看起來我必須走這條路。我會接受答案,因爲它絕對有效。 –