根据iPhone方向旋转UIImageView [英] Rotate UIImageView Depending on iPhone Orientation
本文介绍了根据iPhone方向旋转UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何做到这一点,我想做的只是旋转一个 UIImageView
取决于iPhone的方向。
How would I do this, all I want to do is rotate a UIImageView
depending on the orientation of the iPhone.
推荐答案
您可以通过IB,通过纵向和横向布局获得应用程序,或者您可以通过编程方式。
You can do this through IB, to get an app with a portrait and a landscape layout, or you can do it programmatically. This is about the programmatic way.
要获得更改方向的通知,请使用
To get notifications on the change of orientation, use
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
并添加一个这样的函数(注意这是一个项目复制的, ,您需要根据您的具体情况调整转换)
and add a function like this (note this is copypasted from a project and a lot of lines are left out, you will need to tune the transformation to your specific situation)
-(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];
这篇关于根据iPhone方向旋转UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文