【发布时间】:2013-08-23 12:52:18
【问题描述】:
如何像 youtube 应用一样更改方向。
当我点击此按钮时,如果视图处于纵向模式,它会以横向模式旋转,或者如果视图处于横向模式,它会以纵向模式旋转,当我改变方向时它也可以工作。
【问题讨论】:
-
改变 UIView 纵向和横向的方向..
如何像 youtube 应用一样更改方向。
当我点击此按钮时,如果视图处于纵向模式,它会以横向模式旋转,或者如果视图处于横向模式,它会以纵向模式旋转,当我改变方向时它也可以工作。
【问题讨论】:
试试这个
首先导入这个:
#import <objc/message.h>
比你的按钮方法使用这个
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
objc_msgSend([UIDevice currentDevice],@selector(setOrientation:),UIInterfaceOrientationLandscapeLeft );
}else
{
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientation);
}
}
【讨论】:
实际上 Youtube 的界面并没有真正旋转,他们只是将视频层全屏呈现并旋转层。
旋转设备时也会发生同样的想法,它们使视频层充满屏幕并根据设备旋转进行旋转。 Facebook 在全屏查看照片时也是如此,旋转设备只会旋转视图。
您可以通过要求UIDevice 生成设备符号方向通知来开始监控旋转:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
然后在-(void)orientationChanged: 方法中更改UI:
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
CGAffineTransform transfrom;
CGRect frame = self.videoView.frame;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
transfrom = CGAffineTransformIdentity;
frame.origin.y = 10.0f;
frame.origin.x = 10.0f;
frame.size.width = [UIScreen mainScreen] bounds].size.width;
frame.size.height = 240.0f;
break;
case UIDeviceOrientationLandscapeLeft:
transfrom = CGAffineTransformMakeRotation(degreesToRadians(90));
frame.origin.y = 0.0f;
frame.origin.x = 0.0f;
frame.size.width =[UIScreen mainScreen] bounds].size.height;
frame.size.height =[UIScreen mainScreen] bounds].size.width;
break;
case UIDeviceOrientationLandscapeRight:
transfrom = CGAffineTransformMakeRotation(degreesToRadians(-90));
frame.origin.y = 0.0f;
frame.origin.x = 0.0f;
frame.size.width =[UIScreen mainScreen] bounds].size.height;
frame.size.height =[UIScreen mainScreen] bounds].size.width;
break;
default:
return;
break;
};
[UIView animateWithDuration:0.3f animations: ^{
self.videoView.frame = frame;
self.videoView.transform = transfrom;
}];
}
此代码未经测试而编写,只是为了让您了解如何执行此代码。
【讨论】: