【发布时间】:2011-06-20 14:25:22
【问题描述】:
我正在尝试在点击按钮时向主视图添加子视图。
主视图支持横向模式和纵向模式。
子视图只支持横向模式。
我该怎么做?
【问题讨论】:
标签: iphone
我正在尝试在点击按钮时向主视图添加子视图。
主视图支持横向模式和纵向模式。
子视图只支持横向模式。
我该怎么做?
【问题讨论】:
标签: iphone
在- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 中,您可以对不希望旋转的视图应用转换:
view.transform = CGAffineTransformMakeRotation(M_PI/2);
你可以把它放在一个动画块中,让它跟随旋转动画:
[UIView setAnimationDuration:duration];
[UIView beginAnimations:nil context:NULL];
view.transform = CGAffineTransformMakeRotation(M_PI/2);
[UIView commitAnimations];
旋转发生在视图原点周围,这会使定位变得棘手。为了正确定位视图,我首先会忽略旋转并确保设置支柱和弹簧,以便视图处于正确位置而无需旋转。正确定位视图后,重新启用旋转并调整视图框架的原点,使视图位于您想要的位置。可以应用平移转换而不是调整框架(转换可以与CGAffineTransformConcat 结合使用)。使用哪种方法生成最易读的代码。
小心,这种视图操作会让人感到困惑。从技术上讲,您不想旋转的视图实际上是正在旋转的视图!您可能还需要根据开始和结束方向更改旋转量。
【讨论】:
为您想要添加的所有横向视图获取标签值,然后在将它们添加到主视图之前检查标签值并添加它们。
只要调用 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation,您就可以通过维护一个标志来了解当前方向。
【讨论】:
您是否点击了此链接
iPhone app in landscape mode, 2008 systems
或者只是将这行代码添加到要以横向模式显示的 viewController 的 .m 文件中
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
return YES;
}
return NO;
}
【讨论】:
should rotate时返回YES,当视图should not rotate时返回NO
UIInterfaceOrientationLandscapeLeft 和纵向模式
我通过旋转屏幕和调整视图框架来解决这个问题。
if (toorientation == UIInterfaceOrientationPortrait) {
sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(0));
}
else if (toorientation == UIInterfaceOrientationPortraitUpsideDown){
sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(180));
}
else if (toorientation == UIInterfaceOrientationLandscapeLeft){
sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(-90));
}
else if (toorientation == UIInterfaceOrientationLandscapeRight){
sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(90));
}
【讨论】: