UIViewController shouldAutorotateToInterfaceOrientation - 想添加滞后

时间:2011-10-03 03:32:32

标签: iphone ios ipad accelerometer autorotate

我想推迟自动旋转用户界面,直到设备在一个方向上停留了几秒钟,而不是在他们将设备错误地偏离轴几度时,推动用户疯狂并且不知不觉地轻弹。

我最接近这个(这绝不是我想要的,因为它锁定了UI)是:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.

    [NSThread sleepForTimeInterval:2.0];
    return YES;
}

我想做的是使用这样的东西 - 原则上,通过检查控制台日志,但我需要已注释掉的相应代码行。

-(void) deferredAutorotateToInterfaceOrientation:(NSTimer *) timer { 
    autoRotationTimer = nil;

    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)[timer.userInfo integerValue];

    NSLog(@"switching to new orientation %d now",interfaceOrientation);

    // replace this with code to induce manual orientation switch here.
    //[self forceAutoRotateToInterfaceOrientation:interfaceOrientation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    [autoRotationTimer invalidate];

    autoRotationTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self 
                                   selector:@selector(deferredAutorotateToInterfaceOrientation:) userInfo:[NSNumber numberWithInt:(int)interfaceOrientation ] repeats:NO];

    NSLog(@"denying autorotate, deffering switch to orientation %d by 2 seconds",interfaceOrientation);

    return NO;
}

我意识到有时候有很多方法可以做,所以如果这种方法不是最有效的,而且有人可以提出另一种方法来做到这一点,我全都听见了。我的主要标准是我想延迟自转的开始,同时保持一个响应的用户界面,如果他们只是略微向左倾斜,因为他们在一个刚刚转弯的公交车等。

编辑:我找到了一个可能不适合应用程序存储的解决方案,但是我距离完成还有几个星期,有人可能会在此期间回答这个问题。这个工作调用一个未记录的方法。 (UIPrintInfoOrientation)类型转换只是为了抑制编译器警告,并不影响传递的值。

-(void ) forceUIOrientationInterfaceOrientation:(UIDeviceOrientation) interfaceMode {
    [(id)[UIDevice currentDevice] setOrientation:(UIPrintInfoOrientation) interfaceMode];
}

完整实施,包括重新入学否定如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    acceptNewAutoRotation = YES;
}

-(void ) forceUIOrientationInterfaceOrientation:(UIDeviceOrientation) interfaceMode {
    [(id)[UIDevice currentDevice] setOrientation:(UIPrintInfoOrientation) interfaceMode];
}

-(void) deferredAutorotateToInterfaceOrientation:(NSTimer *) timer { 
    autoRotationTimer = nil;

    acceptNewAutoRotation = YES;

    [self forceUIOrientationInterfaceOrientation:[[UIDevice currentDevice] orientation]];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    [autoRotationTimer invalidate];

    if (acceptNewAutoRotation) {
        autoRotationTimer = nil;
         acceptNewAutoRotation = NO;
        return YES;
    } else {     

        autoRotationTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self 
                                                           selector:@selector(deferredAutorotateToInterfaceOrientation:) userInfo:[NSNumber numberWithInt:(int)interfaceOrientation ] repeats:NO];


        return  NO;
    }    

}

2 个答案:

答案 0 :(得分:0)

要使用公共API执行此操作,您可能不得不忘记自动旋转,并根据已过滤(不仅仅是延迟!)加速计输入手动执行所有自己的视图转换。

答案 1 :(得分:0)

我没有对此进行过测试,但它可能根本不起作用,但你可以尝试一下:

然后开始self.rotate = NO;

- (void)shouldRotateTo:(UIInteraceOrientation *)interfaceOrientation {
    self.rotate = YES;
    // or test interfaceOrientation and assign accordingly.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    bool rot = self.rotate;
    self.rotate = NO
    [self performSelector:selector(shouldRotateTo:) withObject:[NSNumber numberWithInt:interfaceOrientation] afterDelay:2.0];
    return rot;
}

参数interfaceOrientation是枚举(UIInteraceOrientation),因此在传递时将其包装在NSNumber中。