我该如何做到这一点,我只想根据iPhone的方向旋转UIImageView
。
您可以通过IB来获取一个具有纵向和横向布局的应用程序,也可以通过编程方式实现。以下是关于编程方式的内容。
如果想要获取方向改变的通知,请使用:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
然后添加一个类似于这样的函数(注意,这是从一个项目中复制粘贴的,有很多行被省略了,你需要根据你具体的情况来调整转换)
-(void)orientationChanged
{
UIDeviceOrientation o = [UIDevice currentDevice].orientation;
CGFloat angle = 0;
if ( o == UIDeviceOrientationLandscapeLeft ) angle = M_PI_2;
else if ( o == UIDeviceOrientationLandscapeRight ) angle = -M_PI_2;
else if ( o == UIDeviceOrientationPortraitUpsideDown ) angle = M_PI;
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.7];
self.rotateView.transform = CGAffineTransformRotate(
CGAffineTransformMakeTranslation(
160.0f-self.rotateView.center.x,
240.0f-self.rotateView.center.y
),angle);
[UIView commitAnimations];
}
当你完成时,可以像这样停止通知:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.rotateView
设为你想要旋转的子视图即可。 - mvdsM_PI_2
而不是M_PI/2.0f
。 - Vincent Guerci