根据设备方向移动CGPoint

时间:2012-01-15 17:11:10

标签: iphone core-graphics

我有一个以iPhone纵向方向设置的CGPoint。当iPhone移动到横向时,我该如何转换这一点,以便它保持其位置与屏幕边缘成比例?又回来了?

2 个答案:

答案 0 :(得分:0)

试试这个辅助方法:

- (CGPoint)adjustTouch:(CGPoint)aTouch forOrientation:(UIInterfaceOrientation)aOrientation; {

  CGPoint touchLocation = CGPointZero;

  switch (aOrientation) {
    case UIInterfaceOrientationPortrait:
      touchLocation = aTouch;
      break;
    case UIInterfaceOrientationPortraitUpsideDown:
      touchLocation.x = aTouch.x;
      touchLocation.y = 480 - aTouch.y;
      break;
    case UIInterfaceOrientationLandscapeLeft:
      touchLocation.x = aTouch.y;
      touchLocation.y = aTouch.x;
      break;
    case UIInterfaceOrientationLandscapeRight:
      touchLocation.x = 480 - aTouch.y;
      touchLocation.y = 320 - aTouch.x;
      break;
    default:
      break;
  }
  return touchLocation;
}

您可能需要根据需要调整每种情况下的值,因为我不太确定“与屏幕边缘成比例”的含义,但这应该可以帮助您解决问题。希望有帮助!

答案 1 :(得分:-1)

在View Controller中,将CGPoint作为实例变量并覆盖此方法(在UIViewController文档中查看):

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

您需要一些代码来计算新点,以使其按您想要的方式成比例。允许状态栏,我认为这是你需要的(假设我的数学没问题):

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if ( (self.interfaceOrientation == UIInterfaceOrientationPortrait) || (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) ) {
        myPoint = CGPointMake(320/460*myPoint.x, 460/320*myPoint.y);
    }
    else {
        myPoint = CGPointMake(460/320*myPoint.x, 320/460*myPoint.y);
    }
}