3

您好,在我的应用程序中,我将一些 uiButtons 作为子视图添加到如下图所示的视图中。 替代文字 http://img99.imageshack.us/img99/5244/portraitc.png

当用户横向旋转手机时,视图和按钮的位置必须更改为: 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;

}

你有过类似的问题吗?提前谢谢你,索林

4

3 回答 3

1

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

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

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


我只是在一些视图挖掘之后才弄清楚。因此,我正在旋转 viewController.view 并在 viewController.view 的子视图上执行一系列 setNeedsLayout。但是,我需要做的是 [self.navigationController.view setNeedsLayout]。那个电话为我解决了一切。无论出于何种原因,navigationController.view 都没有将触摸事件传递给先前隐藏的子视图部分(就框架/绑定而言)。这实际上也解决了许多其他问题。

于 2010-10-06T17:36:23.313 回答
0

我认为你的问题在于你的框架调整。我的直觉告诉我,这与内部的逻辑有关,shouldAutorotateToInterfaceOrientation因为这发生在轮换实际完成之前。尝试按照他们在本文中的方式进行数学运算,看看是否有帮助。

于 2009-08-19T15:10:03.980 回答
0

我已经解决了我遇到的问题..我基本上需要做[self.navigationController.view setNeedsLayout]

我理解这一点的方式(可能不正确的是,它self.navigationController.view.frameself.view.frame(x = 0,y = 0,宽度 = 320,高度 = 480)相同。然后我旋转self.viewM_PI/2在选择时进行了一些框架操作self.view.subviews 让一切正确地设置动画/位置/缩放。

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

无论如何,在导航控制器的视图上设置 setNeedsLayout 解决了这个问题。我希望这对其他人有帮助。我怀疑这也是 SorinA 遇到的问题,因为按钮没有获得触摸事件。

于 2010-10-06T21:15:50.717 回答