iphone - 防止旋转过程中界面发生变化

标签 iphone ios objective-c orientation

我想支持除纵向之外的所有方向。我想做一些简单的事情,但一直找不到解决方案。

我的界面中有 6 个大按钮。再加上 2 个额外的小按钮。

当方向改变时,我想将所有 8 个按钮保持在相同的中心/位置,我只是想旋转 6 个大按钮,这样它们就会面向正确的方向。

我尝试设置

- (BOOL)shouldAutorotate
{
    return NO;
}

并向自己发送通知,但我必须处理旧方向与新方向才能旋转到正确的位置。还有其他的可能吗?另外,我永远无法获得以前的方向,因为通知是在方向更改后发送的(UIDeviceOrientationDidChangeNotification)

最佳答案

这是我在 View 旋转时用来旋转按钮图像的方法:

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

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

- (void)handleDeviceOrientationDidChangeNot:(NSNotification *)not {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    CGFloat angle = 0.0;
    switch (orientation) {
        case UIDeviceOrientationPortrait:
            angle = 0.0;
            break;
        case UIDeviceOrientationLandscapeLeft:
            angle = M_PI/2;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            angle = M_PI;
            break;
        case UIDeviceOrientationLandscapeRight:
            angle = -M_PI/2;
            break;
        default:
            return;
            break;
    }

    [UIView animateWithDuration:0.35 animations:^{
        self.someButton.imageView.transform = CGAffineTransformMakeRotation(angle);
    } completion:^(BOOL finished) {
        //
    }];
}

关于iphone - 防止旋转过程中界面发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906116/

相关文章:

ios - 直到指定星期几的秒数

ios - 即使在 app 为 "Deleted"之后,有没有办法跟踪耗时?

javascript - Objective-C - 1970 年 1 月 1 日之间的秒数 00 :00:00 UTC and a given date

iphone - 如何检查用户是否已在我的iPhone应用程序中订阅

ios - 函数在完成处理程序完成之前完成它的执行

iphone - 带有 super View 的 iOS 统一缩放 subview

iphone - 如果 Xcode 显示警告,应用程序是否会被 App Store 拒绝?

iphone - 如何将iPhone点击手势识别限制在圆形图像内?

ios - MVP: View Controller 可以向另一个 View Controller 的呈现者发送消息吗?

ios - 在 Swift 中解析关系