[英]How to intercept iPhone rotation
in my app I got a Home with several buttons. 在我的应用程序中,我有一个带有多个按钮的主页。 Each button opens a separate view.
每个按钮打开一个单独的视图。 In each view, if I put the device in landscape a help view is shown.
在每个视图中,如果我将设备放在横向,都会显示一个帮助视图。 Everything works fine except that in whatever view I am, if I put the iPhone like to lay on a table (I don't know how to eplain better...) the app exites from that view and comes back to the first view, the Home.
一切正常,除了在任何视图下,如果我将iPhone放在桌子上(我不知道如何更好地摆放……),应用程序都会从该视图退出并返回第一个视图,家。 Here's my code:
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
[self performSelector:@selector (ritardo) withObject:nil afterDelay:5.0f];
}
-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait)
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.view = self.portraitView;
}
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation
== UIInterfaceOrientationLandscapeRight)
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.view = self.landscapeView;
}
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
}
-(void) ritardo {
ruota.image = [UIImage imageNamed:@"RuotaPerAiuto.png"];
}
- (void)viewDidUnload
{
[self setPortraitView:nil];
[self setLandscapeView:nil];
[self setRuota:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
Hi hope you can help me 嗨,希望你能帮助我
EDIT: I modified the -(void)orientationChanged:(NSNotification *)object{ in this way: 编辑:我以这种方式修改了-(void)orientationChanged:(NSNotification *)object {:
-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIDeviceOrientationFaceUp)// || deviceOrientation ==
UIDeviceOrientationFaceDown)
{
return;
}
if (deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation ==
UIInterfaceOrientationPortraitUpsideDown)
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.view = self.portraitView;
}
else
if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==
UIInterfaceOrientationLandscapeRight)
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.view = self.landscapeView;
}
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
}
Now, if I'm in portrait and get the iPhone to faceUp position works good. 现在,如果我是人像并将iPhone置于faceUp位置,则效果很好。 But when I change position from faceUp to portrait it goes back to the home...
但是当我将位置从faceUp更改为portrait时,它又回到了家中...
The problem is that UIDeviceOrientationDidChangeNotification
will be fired independently from shouldAutorotateToInterfaceOrientation
. 问题在于
UIDeviceOrientationDidChangeNotification
将独立于shouldAutorotateToInterfaceOrientation
触发。 And you don't handle the cases UIInterfaceOrientationPortraitUpsideDown
, UIDeviceOrientationFaceUp
and UIDeviceOrientationFaceDown
in the method orientationChanged:
. 而你不处理的情况下
UIInterfaceOrientationPortraitUpsideDown
, UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
的方法orientationChanged:
So when the device is rotated to one of these orientations, your code is equivalent to: 因此,将设备旋转到这些方向之一时,您的代码等效于:
-(void)orientationChanged:(NSNotification *)object
{
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
}
So you should remove the lines: 因此,您应该删除以下行:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification" object:nil];
and put you code in the method willRotateToInterfaceOrientation
: 并将代码放在
willRotateToInterfaceOrientation
方法中:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
self.view = self.portraitView;
} else {
self.view = self.landscapeView;
}
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
}
Edit 编辑
If you want to keep orientationChanged
, then modify it as follows: 如果要保留
orientationChanged
,请按如下所示进行修改:
-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait) {
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.view = self.portraitView;
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
} else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation == UIInterfaceOrientationLandscapeRight) {
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.view = self.landscapeView;
[UIView commitAnimations];
[self dismissModalViewControllerAnimated:NO];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.