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

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!!

#0

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
}

推荐文章

如果使用getimagesize()的图像超过1000px,则为PHP

如果使用getimagesize()的图像超过1000px,则为PHP

推荐文章

jax-ws和jetty(使用maven)

jax-ws和jetty(使用maven)

推荐文章

使用预编译模板把手.js(jQuery移动环境)

使用预编译模板把手.js(jQuery移动环境)

推荐文章

将下拉选定值作为查询字符串传递到URL

将下拉选定值作为查询字符串传递到URL

推荐文章

无法导入名称objectify

无法导入名称objectify

推荐文章

在C中循环使用字母表输出到Excel的最佳方法是什么?

在C中循环使用字母表输出到Excel的最佳方法是什么?

推荐文章

服务于servlet容器中上下文目录不同的文件夹中的文件

服务于servlet容器中上下文目录不同的文件夹中的文件

推荐文章

Java:Vector警告

Java:Vector警告

推荐文章

HTTP文件上传是如何工作的?

HTTP文件上传是如何工作的?

推荐文章

在多线程应用程序中读/写自定义映射时堆损坏

在多线程应用程序中读/写自定义映射时堆损坏

推荐文章

PHP-尝试创建一个复选框数组,

PHP-尝试创建一个复选框数组,

推荐文章

spring mvc,在DispatcherServlet中找不到映射,带有批注的基本包

spring mvc,在DispatcherServlet中找不到映射,带有批注的基本包

推荐文章

jQuery:将处理程序附加到浏览器的本机自动完成选择

jQuery:将处理程序附加到浏览器的本机自动完成选择

推荐文章

iOS-OAuth Twitter引擎-无法创建令牌

iOS-OAuth Twitter引擎-无法创建令牌

推荐文章

用jquery动态构建表单

用jquery动态构建表单

推荐文章

Rails功能测试:如何测试Admin::PostsController

Rails功能测试:如何测试Admin::PostsController