加载后旋转视图

时间:2011-01-18 10:49:38

标签: iphone cocoa-touch rotation

在我的一个应用程序中,我实现了一个可以播放电影的视图。视图的定位是肖像。我会覆盖以下方法,以便在播放电影后视图将旋转。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (isPlayingMovie) {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
                interfaceOrientation == UIInterfaceOrientationLandscapeLeft );
    } else {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }  
}

问题是视图不会自动旋转。我认为只有在加载时视图才会在视图控制器中调用此方法,但我怎样才能强迫他再次调用它?

提前致谢。

2 个答案:

答案 0 :(得分:1)

简短回答:不可能。

答案很长:只需返回你只做肖像并自己旋转视图。

获取轮换通知(将其放入viewDidAppear或其他内容):

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(rotationChange:)                                                 
     name:UIDeviceOrientationDidChangeNotification
     object:nil];

然后在“rotationChange:”中进行旋转(此处为webview的示例):

- (void)rotationChange:(NSNotification *)notification {
    UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
    CGFloat rotation;
    [self.webView setTransform:CGAffineTransformIdentity];
    switch (o) {
        case UIDeviceOrientationPortrait:
            rotation = 0;
        case UIDeviceOrientationPortraitUpsideDown:
            rotation = 180;
    ...
    }

    [[UIApplication sharedApplication] setStatusBarOrientation:o animated:YES];
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformRotate(transform, rotation * (M_PI / 180.0f));
    [self.webView setTransform:transform];
}

编辑:如果您不需要,请不要忘记禁用通知(例如“viewDidDisappear:”)

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

答案 1 :(得分:0)

尝试使用此方法中的代码

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration{
//required functionality

}

干杯

相关问题