开发者

Why I cannot get the frame of a UIView in order to move it?

I am creating a nav-based app with a view that floats at the bottom of the screen (Alpha .7 most of the time).

I create it like this...

// 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];

this all works (i am not sure about the order i create the uiview in ... meaning i am creating the frame, the view, cr开发者_运维技巧eating the "accuracy text" and adding that to the view, then adding the uiview as a subview of the window . It works and seems correct in my logic.

So, here is the tough part.

I have a timer that i am testing with. I am trying to float the uiview up by 30 pix.

here is that code:

[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];

The problem? rect is nill, rect.origin.x and rect.origin.y are both zero.

Can anyone tell me why?

Here is how i set up self.locationManagerAccuracyUIView in my files:

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: Other places in another timer i DO set the alpha to fade in and out and THAT works! So locationManagerAccuracyUIView is valid and defined as a view...

For instance:

[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];

...and it DOES work. Can anyone help me?

As an aside: I know, when typing this I used self.locationManagerAccuracyUIView and locationManagerAccuracyUIView interchangeably to see if for some reason that was the issue. It is not. :) Thx


First: Rect is null because you are using a %@ to format it, which prints objects and it is not an object, it is a struct. If you want to print it out, convert it to a string using NSStringFromCGRect(rect) and print that instead. The only reason you're not crashing at that point is because the x-coordinate is stored first in the struct, and is equal to zero, which is the same as the null pointer.

Second, your origin is printing out as 0 possibly because you are using %d to format the x and y values, which prints out a signed decimal integer, but they are in fact decimal floating-point values.

Third: The frame of the view is not returned by reference. After you change the rect, you have to reassign it to the view. That is, this:

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

should be this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜