为什么我无法获得UIView的框架以便移动它?

时间:2010-04-12 19:57:29

标签: iphone cocoa-touch uiview

我正在创建一个基于导航的应用程序,其视图浮动在屏幕的底部(Alpha .7大部分时间)。

我这样创造......

// 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是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

1 个答案:

答案 0 :(得分:2)

首先:Rect是null因为你使用%@来格式化它,它打印对象而它不是一个对象,它是一个结构。如果要打印出来,请使用NSStringFromCGRect(rect)将其转换为字符串,然后打印出来。你没有崩溃的唯一原因是因为x坐标首先存储在struct中,并且等于零,这与空指针相同。

其次,您的原点打印为0可能是因为您使用%d格式化x和y值,这会打印出带符号的十进制整数,但它们实际上是十进制浮点值。

第三:视图的框架不是通过引用返回的。更改矩形后,必须将其重新分配给视图。就是这样:

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

应该是这样的:

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