Xcode Objective C:手势后锁定工具栏中的风景

Xcode Objective C: Locking Landscape in a Toolbar after a gesture

我想创建一个具有锁定横向方法和解锁方法的工具栏。我该怎么做? 我也只希望它在滑动手势后显示。我想出了滑动手势,但无法弄清楚用户按下按钮后如何锁定横向。 这是一个具有自动旋转功能的应用程序,并希望保持它直到用户想要锁定横向。我应该在哪里调用或创建这个方法?我是否需要创建另一个 UIViewController 并在主视图中调用它?

您可以覆盖 UI 上的 shouldAutorotate 方法ViewController。

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621419-shouldautorotate?changes=_3&language=objc

这里是 ViewController 一键 lock/unlock 旋转的示例。

@interface ViewController (){
    bool rotationLocked;
}

@property (weak, nonatomic) IBOutlet UIButton *lockButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)togleRotationLock:(id)sender {
    rotationLocked = !rotationLocked;
    if (rotationLocked)
        [_lockButton setTitle:@"Ulock Rotation" forState:UIControlStateNormal];
    else
        [_lockButton setTitle:@"Lock Rotation" forState:UIControlStateNormal];
}

- (bool)shouldAutorotate{
    return !rotationLocked;
}

@end

此外,如果您需要设置特定方向,您可以覆盖 supportedInterfaceOrientations。

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations?changes=_3&language=objc