gpt4 book ai didi

iphone - 程序化 View 正确旋转

转载 作者:行者123 更新时间:2023-11-29 04:34:43 26 4
gpt4 key购买 nike

我几乎所有的用户界面都是以编程方式完成的,并在 Interface Builder 中进行了轻微的修改。但是 99% 的 UI 完全是用代码完成的,因为我觉得通过以下方式可以获得一定程度的灵 active 这样做。

但是,我现在在处理设备旋转方面遇到问题,因为我有几个 UIView 添加为 subview ,所以我面临旋转问题,因为这就是我通常声明 View 的方式

htmlTest.webViewTest.frame = CGRectMake(4.0, 4.0, 312.0, 363.0);

由于这个固定的 CGRectMake,当设备旋转时, View 保持相同的大小并且不适合 View 的方向。

所以我研究了一个在我看来很糟糕的解决方案。我为几个 View 添加了动画,用户可以从中选择选项,然后我将它们动画化。但是他们需要能够处理以纵向或横向加载,然后在加载时,它们需要能够处理从任一方向到另一个方向的旋转。

这就是我完成其中一个 View 的方式。

#pragma createAwesomeJumpBar
- (void)jumpBarButtonPosition:(int)changeView
{
// ChangeView is used to check if the this method is being called from a device rotation or from a button press (0, being rotation and 1, being tabbarButton touch

// if tabbar selected
if (changeView == 1) {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
if (![jumpBarContainerPortrait superview]) {
// load portrait view
jumpBarContainerPortrait = [[UIView alloc] initWithFrame:CGRectMake(0.0, 480.0, 320, (jumpBarHeightPortrait + 49.0))];
jumpBarContainerPortrait.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

// add jumpbar container to view
[self.view insertSubview:jumpBarContainerPortrait belowSubview:actionTabBar];

[UIView animateWithDuration:0.6
delay:0.0f
options:UIViewAnimationCurveEaseIn
animations:^{

jumpBarContainerPortrait.frame = CGRectMake(0.0, (367 - jumpBarHeightPortrait), 320.0, (jumpBarHeightPortrait + 49.0)); // display jumpBar


} completion:^(BOOL finished) {
if (finished) {
NSLog(@"YAY!");
}
}];
}
else if ([jumpBarContainerPortrait superview]) {
//unload portrait view
[UIView animateWithDuration:0.6
delay:0.0f
options:UIViewAnimationCurveEaseIn
animations:^{

jumpBarContainerPortrait.frame = CGRectMake(0.0, 480.0, 320.0, (jumpBarHeightPortrait + 49.0)); // display jumpBar

// remove selected tabButton highlight
[actionTabBar setSelectedItem:nil];


} completion:^(BOOL finished) {
if (finished) {

// remove subView for superView
[jumpBarContainerPortrait removeFromSuperview];

}
}];

}

}
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
{
if (![jumpBarContainerLandscape superview]) {
// load landscape view
jumpBarContainerLandscape = [[UIView alloc] initWithFrame:CGRectMake(0.0, 320, 480.0, (jumpBarHeightLandscape + 49.0))];
jumpBarContainerLandscape.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

// add jumpbar container to view
[self.view insertSubview:jumpBarContainerLandscape belowSubview:actionTabBar];

[UIView animateWithDuration:0.6
delay:0.0f
options:UIViewAnimationCurveEaseIn
animations:^{

jumpBarContainerLandscape.frame = CGRectMake(0.0, (207 - jumpBarHeightLandscape), 480.0, (jumpBarHeightLandscape + 49.0)); // display jumpBar


} completion:^(BOOL finished) {
if (finished) {
NSLog(@"YAY!");
}
}];
}
else if ([jumpBarContainerLandscape superview]) {
// remove landscape view
[UIView animateWithDuration:0.6
delay:0.0f
options:UIViewAnimationCurveEaseIn
animations:^{

jumpBarContainerLandscape.frame = CGRectMake(0.0, 320, 480.0, (jumpBarHeightLandscape + 49.0)); // display jumpBar

[actionTabBar setSelectedItem:nil];

} completion:^(BOOL finished) {
if (finished) {

// remove subView for superView
[jumpBarContainerLandscape removeFromSuperview];
}
}];
}

}
}
// if device rotated selected
else if (changeView == 0) {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
if([jumpBarContainerLandscape superview])
{
// Device is changing from landscape to protrait change views to fit
// load landscape view
jumpBarContainerPortrait = [[UIView alloc] initWithFrame:CGRectMake(0.0, (367 - jumpBarHeightPortrait), 320.0, (jumpBarHeightPortrait + 49.0))];
jumpBarContainerPortrait.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
jumpBarContainerPortrait.alpha = 1.0;

// add jumpbar container to view

[UIView transitionFromView:jumpBarContainerLandscape
toView:jumpBarContainerPortrait
duration:animationSpeed
options:UIViewAnimationOptionTransitionCrossDissolve
completion:NULL];

[self.view insertSubview:jumpBarContainerPortrait belowSubview:actionTabBar];


}
}
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
{
if ([jumpBarContainerPortrait superview])
{
// Device is changing from portrait to landscape change views to fit
// load landscape view
jumpBarContainerLandscape = [[UIView alloc] initWithFrame:CGRectMake(0.0, (207 - jumpBarHeightLandscape), 480.0, (jumpBarHeightLandscape + 49.0))];
jumpBarContainerLandscape.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
jumpBarContainerLandscape.alpha = 1.0;

// add jumpbar container to view

[UIView transitionFromView:jumpBarContainerPortrait
toView:jumpBarContainerLandscape
duration:animationSpeed
options:UIViewAnimationOptionTransitionCrossDissolve
completion:NULL];

[self.view insertSubview:jumpBarContainerLandscape belowSubview:actionTabBar];



}

}
}
}

在这个例子中,我有两个 View 横向和纵向,显然每个名称都代表各自的方向..上面的逻辑沿着这个思路

if tabbarselected 

if !view visible
if device orientation portrait
animate in portrait view.
if device orientation landscape
animate in landscape view

if view visible
if device orientation portrait
animate out portrait view
clear tabbar
if device orientation landscape
animate out landscape view
clear tabbar

if !tabbarselected //meaning listener has identified orientation of device has changed

if device orientation portrait
unload portrait
load landscape

if device orientation landscape
unload landscape
load portrait

我想知道是否有比经历所有这些麻烦更简单的方法!我仍然相当缺乏经验,所以这是我最好的尝试。我希望有人知道一种更简单的方法,而不是必须做所有这些跑腿工作才能将 View 添加到其他 View 中,作为 subview 正确调整方向

任何帮助将不胜感激!我很绝望哈哈:)

最佳答案

请参阅 autoresizingMask documentation 。为您提供与 Interface Builder 中相同的 Spring 和支柱控制。例如:

CGRect frame = CGRectMake(margin, margin, self.view.frame.size.width - margin * 2, self.view.frame.size.height - margin * 2);
UIView *mySubview = [[UIView alloc] initWithFrame:frame];
[self.view mySubview];
mySubview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

此外,如果您认为 autoresizingMask 还不够(例如,当您相对于彼此移动对象以真正微调纵向与横向方向时),我建议您在 viewWillLayoutSubviews 中执行此布局过程对于 iOS5,(或 iOS4 或更早版本中的 willAnimateRotationToInterfaceOrientation)。这样您就不需要自己为更改设置动画,动画将与屏幕旋转动画的其余部分一起完成。

关于iphone - 程序化 View 正确旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11216786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com