Jos*_*hua 8 iphone cocoa-touch objective-c
我该怎么做,我想做的就是UIImageView根据iPhone的方向旋转一个.
mvd*_*vds 18
您可以通过IB执行此操作,以获得具有纵向和横向布局的应用程序,或者您可以以编程方式执行此操作.这是关于程序化的方式.
要获得有关方向更改的通知,请使用
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
并添加一个这样的函数(请注意,这是从项目中复制并且遗漏了很多行,您需要根据具体情况调整转换)
-(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];
}
Run Code Online (Sandbox Code Playgroud)
完成后,停止通知,如下所示:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4192 次 |
| 最近记录: |