如何进行自定义方向锁定操作?

发布于 2024-10-06 14:58:42 字数 712 浏览 0 评论 0原文

我想为我的阅读器应用程序制作一个自定义方向锁定按钮,我想鞭打它不会太糟糕,但可惜我是那个被鞭打的人。

首先,我确实有这个方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
 }

然后我想我可以处理像这样的操作方法中的实际锁定:

- (IBAction) screenLock:(id)sender{

if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait){

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}else{

            [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

  }

但可惜的是,这段代码不会影响指示视图旋转的前者

...我这一切都错了吗?有什么更好的方法呢?我只想有一种本地、简单的方法让我的用户锁定屏幕方向。我猜想他们会使用布尔值,按下按钮锁定,然后再次按下解锁......

有什么想法? 谢谢!!

I would like to make a custom orientation lock button for a reader app of mine, and I was thinking it wouldn't be too bad to whip up, but alas I am the one getting whipped.

To start off I do have this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
 }

And then I was thinking that I could handle the actual locking in an action method like this:

- (IBAction) screenLock:(id)sender{

if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait){

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}else{

            [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

  }

But alas, this code will not hold sway over the former that instructs the view to rotate...

Am I going about this all wrong? What is a better way to do it? I just want to have local, easy way to have my users lock the orientation of their screen. I guess it would be using a boolean value where they hit a button to lock and then hit again to unlock...

Thoughts?
Thanks!!

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

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

发布评论

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

评论(1

城歌 2024-10-13 14:58:42

shouldAutorotateToInterfaceOrientation 会影响您的视图层次结构,因此您的逻辑需要放入应用程序委托中(或者作为可能返回 YES 的最高级 ViewController)。在您的 appDelegate 中放置一个 BOOL 属性,并通过锁定按钮设置它(例如目标指针/delegates (AppDelegate)) 然后在你的 appDelegate 中执行如下操作:

#define ROTATION_MASTER_ENABLED 1

//Setting MASTER_ROTATION_LOCK_ENABLED to 0 will stop the device rotating
//Landscape UP>landscape DOWN and Portrait UP>Portrait DOWN, 
//This is not generally desired or app store safe, default = 1

-(BOOL)compareOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    UIInterfaceOrientation actual = [[UIDevice currentDevice] orientation]; 
    if(UIInterfaceOrientationIsLandscape(interfaceOrientation) && UIInterfaceOrientationIsLandscape(actual))return YES; 
    else if(UIInterfaceOrientationIsPortrait(interfaceOrientation)&& UIInterfaceOrientationIsPortrait(actual))return YES;
    else return NO;   

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    if(!MASTER_ROTATION_LOCK_ENABLED)return NO;
    else if(self.rotationEnabled || [self compareOrientation:interfaceOrientation])return YES;
    return NO;//self.rotationEnabled is a BOOL
}

shouldAutorotateToInterfaceOrientation ripples up your view hierarchy so your logic needs to be put into your app delegate (or as the most senior ViewController that might return YES). Put a BOOL property in your appDelegate and set it via your lock button (e.g. target pointers/delegates (AppDelegate)) then in your appDelegate do something like this:

#define ROTATION_MASTER_ENABLED 1

//Setting MASTER_ROTATION_LOCK_ENABLED to 0 will stop the device rotating
//Landscape UP>landscape DOWN and Portrait UP>Portrait DOWN, 
//This is not generally desired or app store safe, default = 1

-(BOOL)compareOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    UIInterfaceOrientation actual = [[UIDevice currentDevice] orientation]; 
    if(UIInterfaceOrientationIsLandscape(interfaceOrientation) && UIInterfaceOrientationIsLandscape(actual))return YES; 
    else if(UIInterfaceOrientationIsPortrait(interfaceOrientation)&& UIInterfaceOrientationIsPortrait(actual))return YES;
    else return NO;   

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    if(!MASTER_ROTATION_LOCK_ENABLED)return NO;
    else if(self.rotationEnabled || [self compareOrientation:interfaceOrientation])return YES;
    return NO;//self.rotationEnabled is a BOOL
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文