简体   繁体   English

解锁程序化旋转锁定后,强制iOS ViewController旋转到设备方向

[英]Force iOS ViewController to rotate to device orientation after unlocking programmatic rotation lock

I'm implementing a programmatic rotation lock in my app similar to that found in Amazon's Kindle app: when the device is rotated, a lock button shows; 我正在我的应用程序中实现类似于亚马逊的Kindle应用程序中的程序化旋转锁定:当设备旋转时,锁定按钮显示; press the button and the orientation is locked to the orientation the interface was in when the button was pressed. 按下按钮,方向锁定到按下按钮时界面所在的方向。

After unlocking, I'd like to have the interface rotate to the current device orientation. 解锁后,我想让界面旋转到当前的设备方向。 Say you lock rotation in portrait, rotate the device to landscape left, and then unlock; 假设你以纵向锁定旋转,将设备旋转到横向左侧,然后解锁; I'd like the interface to then rotate to landscape left. 我希望界面然后旋转到横向左侧。 Here's the method that toggles the lock: 这是切换锁的方法:

- (IBAction)toggleRotationLock:(UIButton *)sender {
BOOL rotationLocked = [_defaults boolForKey:@"RotationLocked"];
if (rotationLocked) {   //unlock rotation
    [_defaults setBool:NO forKey:@"RotationLocked"];
    /* force rotation to current device orientation here?
     * ...
     */
} else {    //lock rotation to current orientation
    [_defaults setBool:YES forKey:@"RotationLocked"];
    [_defaults setInteger:self.interfaceOrientation forKey:@"RotationOrientation"];
}
    [_defaults synchronize];
    [self setupRotationLockButton];
}

Any way to do this? 有什么办法吗?

The key is 1) saving the current orientation to user defaults exactly like youre doing 2) all the rest of what you need to do is in your overridden methods of the view controllers you want to be lockable (for ios 6+, supportedInterfaceOrientations ). 关键是1)将当前方向保存为用户默认值,就像你正在做的那样2)你需要做的其余部分是你想要锁定的视图控制器的重写方法(对于ios 6+, supportedInterfaceOrientations )。 Use your saved user defaults to return what orientations youre allowing based on if its locked or not. 使用您保存的用户默认值,根据是否锁定,返回您允许的方向。

Then call attemptRotationToDeviceOrientation To tell your view controllers to call their methods again and reevaluate what rotation they should be in given the devices current rotation. 然后调用attemptRotationToDeviceOrientation告诉视图控制器再次调用它们的方法,并在给定设备当前旋转的情况下重新评估它们应该进行的旋转。

This is how i get it to work, just in case if anybody that comes here wants to look at the code. 这就是我如何使用它,以防万一来这里的人想要查看代码。 :) :)

-(IBAction)lockOrientation:(UIButton*)sender
{
if (orientationLocked) { //Unlock it, "orientationLocked" is a boolean defined in .h
    orientationLocked = NO;
    [sender setTitle:@"Unlocked" forState:UIControlStateNormal];
}
else
{ // Lock it.

    //Save the orientation value to NSDefaults, can just be int if you prefer.
    // "defaults" is a NSUserDefaults also defined in .h

    [defaults  setInteger:[[UIApplication sharedApplication] statusBarOrientation] forKey:@"orientation"];
    orientationLocked = YES;
    [sender setTitle:@"Locked" forState:UIControlStateNormal];
}
} 

- (NSUInteger)supportedInterfaceOrientations{
if (orientationLocked) {

    return = [defaults integerForKey:@"orientation"];
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}

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

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