gpt4 book ai didi

ios - 阻止旋转通知传播到 subview

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:01 24 4
gpt4 key购买 nike

我正在使用设备方向通知来了解设备是否已旋转,以便我可以执行选择器。这是我正在使用的代码:

- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
[self performSegueWithIdentifier:@"landscape" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}

但是,当我导航到其他 subview 并旋转设备时,尽管上述代码位于一个独立于其他 View 所具有的类的类中,但仍会执行选择器。那我怎么能阻止呢?我试过:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

它也没有用。当设备旋转时,我的应用程序实际上会以横向模式弹出另一个 View 。所以在 subview 中,横向 View 出现在我旋转设备时。我该如何解决?

最佳答案

移除 UIDeviceOrientationDidChangeNotification

[[NSNotificationCenter defaultCenter] removeObserver:self name: UIDeviceOrientationDidChangeNotification object:nil];

删除所有通知

[[NSNotificationCenter defaultCenter] removeObserver:self];

关于ios - 阻止旋转通知传播到 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15581128/

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