Objective c 和坐标有点混淆
嗨,我有这样的代码:Objective c 和坐标有点混淆,objective-c,Objective C,嗨,我有这样的代码: - (void)viewDidLoad { [super viewDidLoad]; // Get view bounds CGRect bounds = [self.view bounds]; // Get center point CGPoint center; center.x = bounds.origin.x + bounds.size.width/2.0; center.y = bou
- (void)viewDidLoad
{
[super viewDidLoad];
// Get view bounds
CGRect bounds = [self.view bounds];
// Get center point
CGPoint center;
center.x = bounds.origin.x + bounds.size.width/2.0;
center.y = bounds.origin.y + bounds.size.height/2.0;
NSLog(@"center y = %f", center.y);
}
上面的日志最初打印274.0
那么我有这个方法,
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x
duration:(NSTimeInterval)duration
{
CGRect bounds = [[self view] bounds];
// Get center point
CGPoint center;
center.x = bounds.origin.x + bounds.size.width/2.0;
center.y = bounds.origin.y + bounds.size.height/2.0;
// If the orientation is rotating to Portrait mode...
if (UIInterfaceOrientationIsPortrait(x))
{
NSLog(@"In rotation, center y = %f", center.y);
}
}
上述方法中的center.y
的计算方法与您可能注意到的viewDidLoad中的方法相同。然而,当手机像最初一样先旋转到横向,然后再旋转回纵向时,现在上述方法中的center.y
总是打印230(而不是最初的274.0)——为什么?
什么原因会导致这种情况?有没有理由不使用self.view.center或至少是
cRectgetMidy(bounds)
?还有,你是在想bounds
还是frame
?嗨,大卫,我不确定你的意思,只是我的问题是,由于center.y在viewDidLoad和WillAnimateRotationInterfaceOrientation中的计算方式相似,因此它们在这些方法中为什么不同?(旋转发生后,首先是横向旋转,然后是纵向旋转)??自动调整大小?会旋转还是会旋转?@David Rönnqvist:我不太清楚你的意思,也不清楚这与我在帖子中提到的计算center.y
有什么关系
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
CGRect bounds = [[self view] bounds];
CGPoint center;
center.x = bounds.origin.x + bounds.size.width/2.0;
center.y = bounds.origin.y + bounds.size.height/2.0;
if (UIInterfaceOrientationIsLandscape(fromInterfaceOrientation)){
NSLog(@"In rotation, center y = %f", center.y);
}
}