UIButtons在自动旋转后设置帧后没有响应

问题描述:

你好在我的应用程序中,我有一些uiButtons作为子视图添加到视图中,如下图所示。 alt text http://img99.imageshack.us/img99/5244/portraitc.pngUIButtons在自动旋转后设置帧后没有响应

当用户旋转景观手机的看法和按钮必须改变位置,这一点: alt text http://img193.imageshack.us/img193/5931/landscapeq.png

最初时的观点是在纵向模式的按钮触摸作出反应。如果我倾斜手机的按钮移动,一切都看起来像第二个图像,并按钮响应触摸。如果我将手机倾斜回肖像模式,按钮会向后移动,但它们看起来不错,但不会响应触摸。如果我变回景观的按钮工作...

我的按钮创建这样的:

nextNewsP = [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 40, 10, 25, 25)]; 
[nextNewsP setImage:[UIImage newImageFromResource:@"next_arrow.png"] forState:UIControlStateNormal]; 
[nextNewsP addTarget:self action:@selector(nextNewsAction:) forControlEvents:UIControlEventTouchUpInside]; 
[nextNewsP setShowsTouchWhenHighlighted:TRUE]; 
[bottomTools addSubview:nextNewsP]; 
[nextNewsP release]; 

previousNewsP = [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 85, 10, 25, 25)]; 
[previousNewsP setImage:[UIImage newImageFromResource:@"previous_arrow.png"] forState:UIControlStateNormal]; 
[previousNewsP addTarget:self action:@selector(previousNewsAction:) forControlEvents:UIControlEventTouchUpInside]; 
[previousNewsP setShowsTouchWhenHighlighted:TRUE]; 
[bottomTools addSubview:previousNewsP]; 
[previousNewsP release]; 

bottomTools是一个视图,还加入这样一个子视图:

[self.view addSubview:bottomTools]; 
[bottomTools release]; 

我shouldAutorotateToInterfaceOrientation功能如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 


    [UIView beginAnimations:@"moveViews" context: nil]; 


    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"set frames for portrait"); 
     closeView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 30, 2, 23, 25); 
     newsInfo.frame = CGRectMake(10, 10, [[UIScreen mainScreen] bounds].size.width - 10, 22); 
     internetActivityIndicator.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 55, 5, 18, 18); 
     industryLabel.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2.0 - [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width/2, 3, [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width, 22); 
     bottomTools.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-40, [[UIScreen mainScreen] bounds].size.width, 40); 
     nextNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 40, 10, 25, 25); 
     previousNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 85, 10, 25, 25); 
    } 
    else 
    { 
     NSLog(@"set frames for landscape"); 
     closeView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 30, 2, 23, 25); 
     newsInfo.frame = CGRectMake(10, 10, [[UIScreen mainScreen] bounds].size.height - 10, 22); 
     internetActivityIndicator.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 55, 5, 18, 18); 
     industryLabel.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height/2.0 - [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width/2, 3, [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width, 22); 
     bottomTools.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.width-40, [[UIScreen mainScreen] bounds].size.height, 40); 
     nextNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 40, 10, 25, 25); 
     previousNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 85, 10, 25, 25); 
    } 

    [UIView commitAnimations]; 

return YES; 

}

你曾经有过类似的问题吗? 谢谢你, 索林

我认为你的问题是在你的帧调整。我的直觉告诉我,这与shouldAutorotateToInterfaceOrientation内部的逻辑有关,因为这发生在旋转实际完成之前。尝试按照他们在this article中的方式进行数学计算,看看是否有帮助。

+0

谢谢你的链接。我会尝试一下并返回一个反馈。 Sorin – 2009-08-19 15:19:45

+0

该文章非常有用,谢谢!它的工作:) – 2009-08-20 09:54:06

我实际上仍在努力解决同样的问题(一年后...需要iOS 3.0兼容性)。旋转部分很容易。但是,视图和按钮似乎不处理触摸和移动/拖动事件。我已验证窗口(UIWindow)正在接收触摸事件并在正确的位置。

就好像屏幕的最后1/3(即480-320)在从肖像旋转到横向后,不会将事件传播到接收者。

由于slf链接中的示例没有帮助,因为旋转的视图控制器没有响应者。


我只是在一些视图挖掘后发现它。所以,我旋转了viewController.view,并在viewController.view的子视图上做了一堆setNeedsLayout。但是,我需要做的是[self.navigationController.view setNeedsLayout]。那一个电话为我解决了一切。无论出于何种原因,navigationController.view都没有将触摸事件传递到先前隐藏的子视图部分(就框架/边界而言)。这实际上也解决了一些其他问题。

+0

检查,看看是不是重叠或如果他们已启用用户交互。这些现在进入我的脑海。 – 2010-10-06 18:21:57

+0

娜,不是那样的。我在挖掘一些视图后才发现它。所以,我旋转了viewController.view,并在viewController.view的子视图上做了一堆setNeedsLayout。但是,我需要做的是[self.navigationController.view setNeedsLayout]。那一个电话为我解决了一切。无论出于何种原因,navigationController.view都没有将触摸事件传递到先前隐藏的子视图部分(就框架/边界而言)。这实际上也解决了一些其他问题。 – Ephraim 2010-10-06 21:07:33

我修复了我遇到的问题..我基本上需要做[self.navigationController.view setNeedsLayout]

的方式我明白这一点(这也许不正确是self.navigationController.view.frame是相同self.view.frame两者均为等于(X = 0,Y = 0,宽度= 320,高度= 480)。然后,我通过M_PI/2旋转self.view和在select self.view上做了一些帧操作。子视图让一切都能正确动画/定位/缩放。

结果没问题,但导航控制器不愿意承认触摸事件的部分self.view在320的右边。实质上,如果这个self.navigationController.view.clipsToBounds是真的,它可能甚至没有显示那部分self.view。

无论如何,在导航控制器的视图上设置setNeedsLayout解决了问题。我希望这可以帮助别人。我怀疑这也是SorinA用按钮没有触碰事件的问题。

+0

这是我对下面发布的自己问题的解决方案。 – Ephraim 2010-10-06 21:17:28