繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM