如何进行自定义方向锁定操作?
我想为我的阅读器应用程序制作一个自定义方向锁定按钮,我想鞭打它不会太糟糕,但可惜我是那个被鞭打的人。
首先,我确实有这个方法:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
shouldAutorotateToInterfaceOrientation
会影响您的视图层次结构,因此您的逻辑需要放入应用程序委托中(或者作为可能返回 YES 的最高级 ViewController)。在您的 appDelegate 中放置一个 BOOL 属性,并通过锁定按钮设置它(例如目标指针/delegates (AppDelegate)) 然后在你的 appDelegate 中执行如下操作: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: