gpt4 book ai didi

iphone - 如何在不允许旋转其界面的 Controller 中旋转 View ?

转载 作者:可可西里 更新时间:2023-11-01 17:09:06 25 4
gpt4 key购买 nike

帮我解决这个任务 - 我有一个不允许旋转其界面的 viewController:

- (BOOL)shouldAutorotate {
return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

但我需要旋转出现在该 Controller 中的 alertView!因此,如果用户旋转设备,alertView 应该跟随旋转并且主界面保持静止。我尝试订阅通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];

但在我的 deviceRotated:我收到带有此类有效负载的通知: NSConcreteNotification 0x101dc50 {name = UIDeviceOrientationDidChangeNotification; object = <UIDevice: 0x103e640>; userInfo = {
UIDeviceOrientationRotateAnimatedUserInfoKey = 1;
}}

什么是 UIDeviceOrientationRotateAnimatedUserInfoKey ?我如何使用知道当前的 interfaceOrientation?或者建议一种更好的获取当前方向和旋转 alertView 的方法。

我试过

(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration但是这些方法不会在 iOS6 上被调用:(

此外,是否有任何其他方法可用于了解设备正在旋转到某个方向?提前致谢!附言我知道这个任务看起来有点傻,但这是客户的说法。对不起:)

最佳答案

UIDevice 中有一个orientation 变量,它包含当前设备方向。您可以使用它代替界面方向(不会像您已经注意到的那样旋转)。

首先,您订阅设备方向更改,一个好的地方是 viewWillAppear 并取消订阅 viewWillDisappear:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

在这个例子中,我们表示我们希望在设备旋转时调用 orientationChanged:,所以让我们实现它:

#pragma mark - Orientation change events
- (void)orientationChanged:(NSNotification*)notification {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

// Calculate rotation angle
CGFloat angle;
switch (deviceOrientation) {
case UIDeviceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIDeviceOrientationLandscapeLeft:
angle = M_PI_2;
break;
case UIDeviceOrientationLandscapeRight:
angle = - M_PI_2;
break;
default:
angle = 0;
break;
}

// Apply rotation
static NSTimeInterval animationDuration = 0.3;
[UIView animateWithDuration:animationDuration animations:^{
_viewToRotate.transform = CGAffineTransformMakeRotation(angle);
}];
}

关于iphone - 如何在不允许旋转其界面的 Controller 中旋转 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15390359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com