仅在 ios 中旋转视图

Rotate the view only in ios

我的应用程序仅支持纵向模式。我有要求只需要根据设备方向旋转特定的 UIView

你能帮帮我吗?

在您的 viewdidload 或 init 中添加下面的代码

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

在 deviceOrientationDidChange 函数中,您将获得设备方向,据此您可以更新您的视图

- (void)deviceOrientationDidChange:(NSNotification *)notification {
     //Obtain current device orientation
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

      //Do my thing
 }
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
    // set your view frame work for portrait mode

}
else {
// set new frame of view for landscape mode.
}

希望你明白了。 谢谢

经过长时间的尝试,我找到了使用 CGAffineTransform ..

的答案
- (void)deviceOrientationDidChange:(NSNotification *)notification {
    //Obtain current device orientation
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    CGFloat angle;
    switch (orientation) {
        case UIDeviceOrientationPortraitUpsideDown:
            angle = M_PI;
            break;
        case UIDeviceOrientationLandscapeRight:
            angle = - M_PI_2;
            break;
        case UIDeviceOrientationLandscapeLeft:
            angle = M_PI_2;
            break;
        case UIDeviceOrientationPortrait:
        default:
            angle = 0.f;
            break;
    }
    animationView.transform= CGAffineTransformMakeRotation(angle);
    animationView.frame = self.view.frame;
}