iPhone UIView center property returning bad value
I'm working with a View-based application compiling fo开发者_开发百科r iPhoneOS 4.0 Simulator (Debug), Xcode 3.2.3.
I've got a UIImageView, imgView, whose center I want the coordinates of. I obtain them like this:
CGPoint imgviewcoords=[imgView center];
This doesn't produce any compile-time errors, but when I NSLog the coordinates like this:
NSLog(@"x: %i, y:%i", imgviewcoords.x, imgviewcoords.y);
I get this output:
x: 0, y:108762
It's showing 0 for imgView's x coordinate (which I know isn't right, because imgView is near the top-middle of the screen on Interface Builder) and some giant impossible number which is way past the boundaries of the iPhone's screen for the y coordinate (The y coordinate in the output above may not be exactly correct, but it's some giant number like that). I get this same exact output each time. The imgView is properly linked to its File's Owner outlet, and I can even change its image using
[imgview setImage:[UIImage imageNamed:@"./blahblah.png"]];
I just can't seem to properly get its center coordinates. I've also tried
CGPoint viewcoords=[[imgView frame] origin];
and that gives me the same erroneous coordinates in imgviewcoords as described above. This happens with every control that I have in my app's main UIView, except the y coordinate differs a little bit for each control. What am I doing wrong?
@Vladimir : Thanks for the suggestion to change the NSLog format specifiers. However, I don't think it's the output that is the problem. I think it's the [imgView center] call that isn't working. I'm using the CGPoint that's returned from [imgView Center] to set the center of another UIImageView, and that UIImageView simply moves to the very top-left of the screen instead of moving to the center of imgView. So I'm guessing it's the [imgView center] call that is returning a bad set of coordinates.
%i format specifier expects integer value and CGPoint components have CGFloat type, try to use correct specifier (%f) - may be you will get correct output:
NSLog(@"x: %f, y:%f", imgviewcoords.x, imgviewcoords.y);
精彩评论