设置自动旋转框架后,UIButtons 不响应触摸
您好,在我的应用程序中,我将一些 uiButtons 作为子视图添加到视图中,如下图所示。 替代文本 http://img99.imageshack.us/img99/5244/portraitc.png< /a>
当用户横向旋转手机时,视图和按钮必须将位置更改为: 替代文本 http://img193.imageshack.us/img193/5931/landscapeq.png< /a>
最初,当视图处于纵向模式时,按钮会对触摸做出响应。 如果我倾斜手机,按钮就会移动,一切看起来都正常,如第二张图片所示,并且按钮会对触摸做出响应。 如果我将手机倾斜回纵向模式,按钮会向后移动,它们看起来不错,但不会响应触摸。 如果我改回横向按钮工作...
我的按钮创建如下:
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;
}
您是否遇到过类似的问题? 先感谢您, 索林
Hello in my app i have some uiButtons added as subviews to a view like in the picture below.
alt text http://img99.imageshack.us/img99/5244/portraitc.png
when the user rotates the phone in landscape the views and buttons must change position to this:
alt text http://img193.imageshack.us/img193/5931/landscapeq.png
initially when the view is in portrait mode the buttons respond to touches. If i tilt the phone the buttons move, everything looks ok like in the second image and the buttons respond to touches. If i tilt the phone back to portrait mode, the buttons move back they look ok but they don't respond to touches. If i change back to landscape the buttons work...
my buttons are created like this:
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 is a view and is added also as a subview like this:
[self.view addSubview:bottomTools];
[bottomTools release];
and my shouldAutorotateToInterfaceOrientation function looks like this:
- (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;
}
did you ever had a similar problem?
thank you in advance,
Sorin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我实际上仍在为同样的问题而苦苦挣扎(一年后......需要 iOS 3.0 兼容性)。 旋转部分很容易。 但是,视图和按钮似乎不处理触摸和移动/拖动事件。 我已经验证窗口 (UIWindow) 正在接收触摸事件并且位于正确的位置。
就好像屏幕的最后 1/3(即 480-320)在从纵向旋转到横向后不会将事件传播到接收者。
slf 链接中的示例没有帮助,因为旋转视图控制器没有响应器。
经过一些视图挖掘后我才发现这一点。 因此,我正在旋转 viewController.view 并在 viewController.view 的子视图上执行一系列 setNeedsLayout 操作。 然而,我需要做的是[self.navigationController.view setNeedsLayout]。 那一通电话为我解决了一切。 无论出于何种原因,navigationController.view 都没有将触摸事件传递到之前隐藏的子视图部分(就框架/边界而言)。 这实际上还解决了许多其他问题。
I am actually still struggling with the same problem (a year later ... need iOS 3.0 compatibility). The rotation part was easy. However, the views and buttons don't seem to process touch and move/drag events. I've verified that the window (UIWindow) is receiving the touch events and at the right locations.
It's as if the last 1/3 of the screen (i.e. 480-320) doesn't propagate events to receivers after a rotation to landscape from portrait.
The example in the link by slf doesn't help since the rotated view controller doesn't have responders.
I just figured it out after some view digging. So, I was rotating the viewController.view and doing a bunch of setNeedsLayout on subviews of viewController.view. However, what I needed to do was [self.navigationController.view setNeedsLayout]. That one call fixed everything for me. For whatever reason, the navigationController.view wasn't passing touch events to the part of the subviews that were previously hidden (as far as the frame/bound is concerned). That actually fixed a number of other issues as well.
我认为你的问题出在你的框架调整上。 我的直觉告诉我,这与shouldAutorotateToInterfaceOrientation内部的逻辑有关,因为这发生在旋转实际完成之前。 尝试按照本文中的方法进行数学计算,看看是否有帮助。
I think your problem is in your frame adjustment. My gut tells me it has something to do with the logic being inside
shouldAutorotateToInterfaceOrientation
because that happens before the rotation is actually done. Try doing your math the way they do it in this article and see if that helps.我已经解决了我遇到的问题..我基本上需要做
[self.navigationController.view setNeedsLayout]
。我理解这一点的方式(可能不正确的是, self.navigationController.view.frame 与 self.view.frame 相同,并且两者都等于 (x=0, y=0,width=320,height=480)。然后我将
self.view
旋转了M_PI/2
并在选择 self.view 上进行了一些帧操作。子视图使所有内容都正确地进行动画/位置/缩放,效果很好,但导航控制器不愿意承认 320 右侧 self.view 部分的触摸事件。本质上,如果这个
self .navigationController.view.clipsToBounds 是正确的,它甚至可能没有显示 self.view 的那部分。
无论如何,在导航控制器的视图上设置 setNeedsLayout 解决了这个问题。 SorinA 还遇到按钮无法获取触摸事件的问题。
I've fixed the problem I was having .. I basically needed to do
[self.navigationController.view setNeedsLayout]
.The way I understand this (which maybe incorrect is that
self.navigationController.view.frame
was same asself.view.frame
and both were equal to (x=0,y=0,width=320,height=480). I then rotatedself.view
byM_PI/2
and did a number of frame manipulation on select self.view.subviews to get everything to animate/position/scale correctly.That worked out okay but the navigation controller was not willing to acknowledge touch events to parts of self.view there were to the right of 320. In essence, if this
self.navigationController.view.clipsToBounds
were true, it might not even have shown that part of self.view.Anyway, setting setNeedsLayout on the navigation controller's view resolved the issue. I hope this helps someone else. I suspect that was also the problem SorinA was having with buttons not getting touch events.