2010-04-12 53 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

相关问题