2010-04-12 83 views
0

我正在創建一個基於導航的應用程序,其視圖浮動在屏幕的底部(大多數時間爲Alpha .7)。爲什麼我無法獲得UIView的框架來移動它?

我創建它像這樣...

// stuff to create the tabbar/nav bar. 
// THIS ALL WORKS... 
// then add it to subview. 

[window addSubview:tabBarController.view]; 

// need this last line to display the window (and tab bar controller) 
[window makeKeyAndVisible]; 

// Okay, here is the code i am using to create a grey-ish strip exactly `zlocationAccuracyHeight` pixels high at `zlocationAccuracyVerticalStartPoint` starting point vertically. 

CGRect locationManagerAccuracyUIViewFrame = CGRectMake(0,zlocationAccuracyVerticalStartPoint,[[UIScreen mainScreen] bounds].size.width,zlocationAccuracyHeight); 
self.locationManagerAccuracyUIView = [[UIView alloc] initWithFrame:locationManagerAccuracyUIViewFrame]; 
self.locationManagerAccuracyUIView.autoresizingMask = (UIViewAutoresizingFlexibleWidth); 
self.locationManagerAccuracyUIView.backgroundColor = [UIColor darkGrayColor]; 
[self.locationManagerAccuracyUIView setAlpha:0]; 

CGRect locationManagerAccuracyLabelFrame = CGRectMake(0, 0,[[UIScreen mainScreen] bounds].size.width,zlocationAccuracyHeight); 
locationManagerAccuracyLabel = [[UILabel alloc] initWithFrame:locationManagerAccuracyLabelFrame]; 

if ([myGizmoClass useLocationServices] == 0) 
{ 
locationManagerAccuracyLabel.text = @"GPS Accuracy: Using Manual Location"; 
} 
else 
{ 
locationManagerAccuracyLabel.text = @"GPS Accuracy: One Moment Please..."; 
} 

locationManagerAccuracyLabel.font = [UIFont boldSystemFontOfSize:12]; 
locationManagerAccuracyLabel.textAlignment = UITextAlignmentCenter; 
locationManagerAccuracyLabel.textColor = [UIColor whiteColor]; 
locationManagerAccuracyLabel.backgroundColor = [UIColor clearColor]; 
[locationManagerAccuracyLabel setAlpha:0]; 
[self.locationManagerAccuracyUIView addSubview: locationManagerAccuracyLabel]; 
[window addSubview: self.locationManagerAccuracyUIView]; 

這一切工作(我不知道我在...這意味着我創建的框架,在視圖中創建了UIView的順序,創建「準確的文本」,並補充說,到視圖,然後添加的UIView作爲窗口的子視圖,它的工作原理,並在我的邏輯似乎是正確的。

所以,這裏是最困難的部分。

我有一個定時器,我正在測試。我想浮動uiview由30像素。

這裏是代碼:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 
CGRect rect = [ self.locationManagerAccuracyUIView frame]; 
NSLog(@"ORIGIN: %d x %d (%@)\n",rect.origin.x,rect.origin.y,rect); 
rect.origin.y -= 30; 
[UIView commitAnimations]; 

的問題? rect is nill,rect.origin.x和rect.origin.y都是零。

有誰能告訴我爲什麼?

這是我如何設置在我的文件self.locationManagerAccuracyUIView:

Delegate.h

UIView  *locationManagerAccuracyUIView; 
UILabel  *locationManagerAccuracyLabel; 


... 

@property (nonatomic, retain) IBOutlet UIView *locationManagerAccuracyUIView; 
@property (nonatomic, retain) IBOutlet UILabel *locationManagerAccuracyLabel; 

Delegate.m

... 

@synthesize locationManagerAccuracyUIView; 
@synthesize locationManagerAccuracyLabel; 

... 

BTW:其他地方另一個定時器,我設置alpha淡入和淡出效果!所以locationManagerAccuracyUIView是有效的,定義爲一個視圖...

例如:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[locationManagerAccuracyLabel setAlpha:1]; 
[UIView commitAnimations]; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[self.locationManagerAccuracyUIView setAlpha:.7]; 
[UIView commitAnimations]; 

...和它的工作。誰能幫我?另外:我知道,在輸入這​​個字符時,我互換地使用了self.locationManagerAccuracyUIViewlocationManagerAccuracyUIView以查看是否由於某種原因導致問題出現。不是這樣。 :) Thx

回答

2

第一:由於您使用的是%@來格式化Rect,因爲它打印對象並且它不是對象,所以它是一個結構。如果要打印出來,請使用NSStringFromCGRect(rect)將其轉換爲字符串,然後打印。在這一點上沒有崩潰的唯一原因是因爲x座標首先存儲在結構中,並且等於零,這與空指針相同。

其次,您的原點打印爲0可能是因爲您正在使用%d格式化x和y值,這會打印出帶符號的十進制整數,但實際上它們是十進制浮點值。

第三:視圖的框架不是通過引用返回的。在您更改矩形後,您必須將其重新分配給視圖。也就是說,這樣的:

rect.origin.y -= 30; 
[UIView commitAnimations]; 

應該是這樣的:

rect.origin.y -= 30; 
self.locationManagerAccuracyUIView.frame = rect; 
[UIView commitAnimations]; 
+0

這工作 - 太棒了!我不知道爲什麼我沒有把修剪矩形放回到UIView對象中。感謝您思考並詳細解釋 - 當我沒有想到的時候。已投票接受答案! – Jann 2010-04-12 22:16:31

相關問題