仅在ios中旋转视图

时间:2015-08-05 06:30:55

标签: ios xcode uiinterfaceorientation cgaffinetransform screen-rotation

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

你能帮帮我吗?

3 个答案:

答案 0 :(得分:0)

在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
 }

答案 1 :(得分:0)

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

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

希望你能得到这个想法。 感谢

答案 2 :(得分:0)

经过长时间的尝试,我找到了使用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;
}