【问题标题】:AutoLayout - UIImageView that doesn't rotateAutoLayout - 不旋转的 UIImageView
【发布时间】:2016-05-01 18:33:41
【问题描述】:

如何让UIImageView 在用户旋转手机时不旋转?

视图控制器中的所有其他视图都应该像往常一样使用自动布局进行更新,但我希望UIImageView 保持不变。

我怎样才能做到这一点?现在我正在旋转viewWillTransitionToSize 中的图像,但这看起来很糟糕,并且不考虑LandscapeLeftLandscapeRight 的对比

【问题讨论】:

    标签: ios uiimageview autolayout


    【解决方案1】:

    没有魔法标志可以让 UIView 不旋转,但您可以使用以下代码将其旋转回来:

    -(void) viewDidLoad {
        [super viewDidLoad];
        // Request to turn on accelerometer and begin receiving accelerometer events
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    
    - (void)orientationChanged:(NSNotification *)notification {
        // Respond to changes in device orientation
        switch ([UIDevice currentDevice].orientation)
        {
            case UIDeviceOrientationLandscapeLeft:
                self.img.transform = CGAffineTransformMakeRotation(-M_PI/2);
                break;
            case UIDeviceOrientationLandscapeRight:
                self.img.transform = CGAffineTransformMakeRotation(M_PI/2);
                break;
            case UIDeviceOrientationPortrait:
                self.img.transform = CGAffineTransformMakeRotation(0);
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                self.img.transform = CGAffineTransformMakeRotation(0);
                break;
            case UIDeviceOrientationFaceUp:
                break;
            case UIDeviceOrientationFaceDown:
                break;
            case UIDeviceOrientationUnknown:
                break;
        }
    }
    
    -(void) viewDidDisappear {
        // Request to stop receiving accelerometer events and turn off accelerometer
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    }
    

    【讨论】:

    • 这不会保留图像的确切大小,并且在旋转过程中它似乎也在旋转,这非常草率。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多