加载后旋转视图

发布于 2024-10-12 16:51:58 字数 537 浏览 4 评论 0原文

在我的一个应用程序中,我实现了一个可以播放电影的视图。视图的方向是纵向。我重写了以下方法,以便在电影播放后视图会旋转。

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

问题是视图不会自动旋转。我认为视图仅在加载时在视图控制器中调用此方法,但我如何强制他再次调用它?

提前致谢。

In one of my applications I implemented a view that can play a movie. the oriantation of the view is Portrait. I overwrite the foloowing method so the view will rotate once the movie is playing.

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

The problem is that the view will not automatically rotate. I figured that the view calles this method in the view controller only while it's loading but how can i force him to call it again ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-10-19 16:51:58

简短的回答:不可能。

长答案:只需返回您仅进行纵向并自行旋转视图即可。

获取旋转通知(将其放在 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];

Short answer: not possible.

Long answer: Just return that you do portrait only and rotate the view yourself.

Get rotation notifications (put it in viewDidAppear or something):

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

Then do the rotation in "rotationChange:" (example for a webview here):

- (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];
}

EDIT: Don't forget to disable the notifications if you don't need them (like in "viewDidDisappear:")

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
奶气 2024-10-19 16:51:58

尝试使用此方法中的代码

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

}

干杯

Try using the code in this method

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

}

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文