当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - Core Animation and device orientation

问题描述:

I have a single view application on iOS. I then added an animation on a view.layer's sublayer using CAKeyframeAnimation. It works perfectly at long as the device is upright. As soon as I rotate the device, the animation "rotates" with the device. So a translation left to right becomes a translation top to bottom, and so on, as I turn the device.

The UIViewController's view rotates as it should but the sub-CALayer transformation doesn't.

I can't understand how I need to deal with that. I could counter the device rotation with a rotation of my own, as this post suggests. It just feels like I am missing something. Is there something I need to do to enable this or do I really need to manually handle the rotation?

网友答案:

I did what the post suggested and countered the device orientation with a rotation. Here's the code, for the benefit of others:

- (void)setupDeviceCounterRotation
{
  CATransform3D deviceCounterRotation;

  if(self.interfaceOrientation == UIDeviceOrientationPortrait) {
    deviceCounterRotation = CATransform3DIdentity;
  }
  else if(self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
    deviceCounterRotation = CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f);
  }
  else if(self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) {
    deviceCounterRotation = CATransform3DMakeRotation(M_PI/2, 0.0f, 0.0f, 1.0f);
  }
  else if(self.interfaceOrientation == UIDeviceOrientationLandscapeRight) {
    deviceCounterRotation = CATransform3DMakeRotation(-M_PI/2, 0.0f, 0.0f, 1.0f);
  }

  self.view.layer.sublayerTransform = deviceCounterRotation;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: