Ios 是否可以在设备方向更改时更改控制器内单个视图的方向?
我试图在Ios 是否可以在设备方向更改时更改控制器内单个视图的方向?,ios,objective-c,cocoa-touch,uiview,uiinterfaceorientation,Ios,Objective C,Cocoa Touch,Uiview,Uiinterfaceorientation,我试图在UIViewController中打开单个UIView,而不打开整个控制器。这可能吗?我将试图说明: _____^_____ ____________________ | | | ____^____ | | __^__ | | | | | | | | | | A | B | > | | B | | --> | |_____
UIViewController
中打开单个UIView
,而不打开整个控制器。这可能吗?我将试图说明:
_____^_____ ____________________
| | | ____^____ |
| __^__ | | | | |
| | | | | A | B | >
| | B | | --> | |_________| |
| |___| | | |
| | |____________________|
| A |
|_________|
显示视图的方向时,每个视图的箭头都指向上。UIViewController
(A)始终保持纵向,这样,如果设备转向横向,则向上将位于侧面。ui视图
将转向服从设备的方向,因此up将向上。
在YouTube应用程序中可以看到这一点或类似的内容,如果您在纵向中单击视频,它将显示在视图的顶部,如果您打开设备,则只有视频视图在横向中播放。他们解决问题的方式不同吗?单个
UIView
是否可以有自己的规则,用于shouldAutorotateToInterfaceOrientation
,shouldAutorotate
,支持的接口方向
和/或首选接口方向进行演示
,而不是控制器?您需要自己对视图应用转换。您应该在viewDidLayoutSubviews
中应用此转换,以确保在视图为布局时正确应用该转换,还应注册UIDeviceOrientationIDChangeNotification
通知,并在设备方向更改时重新应用该转换
-(void)_applyTransform
{
CGAffineTransform t = CGAffineTransformIdentity;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
t = CGAffineTransformMakeRotation(M_PI/2.0);
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
t = CGAffineTransformMakeRotation(-M_PI/2.0);
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
t = CGAffineTransformMakeRotation(M_PI);
}
[_view setTransform:t];
}
- (void)_deviceOrientationDidChange:(NSNotification*)n
{
[self _applyTransform];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self _applyTransform];
}
由答案的粉丝添加!:)为了记录在案,这里有一整套典型的工作代码,使用了Leo令人敬畏的示例 假设您有一个UIView类,因此,通常与正在加载的XIB相匹配。在本例中,视图有四个按钮。我们将在iPhone旋转时旋转按钮。(为了明确起见,我们只是在视图中旋转,我们想旋转的东西-在本例中,我们不是旋转整个视图。您可以旋转东西、移动项目、隐藏东西,或者任何您想要的东西,或者如果相关的话,您也可以旋转整个视图。处理更广泛的调整大小问题是不同的,本例显示“只是”将该死的按钮旋转到位。) (顺便说一句,通常我会建议在这里使用可能的类别,但这是一个很好的例子来说明它是如何工作的,就像直接粘贴在代码中一样。) 请注意,它会设置按钮旋转的动画
@implementation SomeUIView
-(id)initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
if (!self) return nil;
// your other setup code
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(spun)
name:UIDeviceOrientationDidChangeNotification object:nil];
NSLog(@"added ok...");
return self;
}
-(void)dealloc
{
NSLog(@"removed ok...");
// your other dealloc code
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIDeviceOrientationDidChangeNotification object:nil];
}
然后旋转按钮(或者你想要的任何东西)
你试过我的解决方案吗?
-(void)spinOneThing:(UIView *)vv
{
CGAffineTransform t = CGAffineTransformIdentity;
if ([[UIDevice currentDevice] orientation] ==
UIDeviceOrientationLandscapeLeft)
t = CGAffineTransformMakeRotation(M_PI/2.0);
if ([[UIDevice currentDevice] orientation] ==
UIDeviceOrientationLandscapeRight)
t = CGAffineTransformMakeRotation(-M_PI/2.0);
if ([[UIDevice currentDevice] orientation] ==
UIDeviceOrientationPortraitUpsideDown)
t = CGAffineTransformMakeRotation(M_PI);
[UIView animateWithDuration:0.1 animations:^{ [vv setTransform:t]; }];
}
-(void)spun // the device was spun by the user
{
NSLog(@"spun...");
[self spinOneThing:self.buttonA];
[self spinOneThing:self.buttonB];
[self spinOneThing:self.buttonC];
[self spinOneThing:self.buttonD];
}