iOS,UIScrollView中的第一页启用横向,第二页禁用横向

时间:2018-11-03 03:41:47

标签: ios objective-c orientation landscape

有一个UIScrollView,它有两页,UIScrollView中的第一页我想启用横向显示,UIScrollView中的第二页我要禁用横向显示,现在我使用下面的代码,但是它的工作尚未完成,第二页将显示水平屏幕,然后显示垂直屏幕:

- (void)viewWillAppear:(BOOL)animated {
    [ITNotificationCenter addObserver:self selector:@selector(handleDeviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)handleDeviceOrientationDidChange{
    if(isFirstPage){
        //landscape code
    } else {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
}

我该如何实现?每个答案将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题已解决。请看下面的代码。

AppDelegate.h文件:

@property (nonatomaic, assign, getter=isNeedRotation) BOOL needRotation;

AppDelegate.m文件:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.isNeedRotation) {
        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

yourUIViewController.m文件:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.mainScrollView) {
        AppDelegate *appDelegate = (AppDelegate *)ITUIApplicationDelegate;
        if (scrollView.contentOffset.x < screen_width) {
            appDelegate.needRotation = YES;
        } else {
            appDelegate.needRotation = NO;
        }
    }
}