【问题标题】:Make UIView move down if iPhone is iPhone 5如果 iPhone 是 iPhone 5,则使 UIView 向下移动
【发布时间】:2013-06-26 10:54:21
【问题描述】:

我目前正在创建一个应用程序,它在视图中包含一个pickerView 和一个tapbar。视图位于屏幕底部。点击栏始终可见。如果我点击点击栏上的按钮,视图将向上移动并显示pickerView。 我的问题是:

视图不会向下移动到 iPhone 5 屏幕的底部。我将此代码放在不同的位置:

CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height > 480.0f){
    [UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
    [UIView setAnimationDuration:0.6];//in seconds
    self.hiddenView.frame = CGRectMake(0, 504, 320, 260);//move the picker to a specific position (504 = 568 - statusbar(20) - tapbar(44))
    [UIView commitAnimations];//tells the compiler to start the animations
}

比如在:

-(void)viewDidLoad{}
-(void)viedWillAppear{}
-(void)viewDidAppear{}

我一无所知。有谁知道如何使它正确吗?

UPADTE

好的,感谢所有答案,但我的动画效果很好。我的问题是它每次都执行(使用断点检查)不会触发(视图不会向下移动)。所以我问 WHERE 我必须把我的代码放在哪里才能正常工作。如果代码正确则不会,因为它是正确的。我知道有不同的方法可以检查它是否是 iPhone 5 并对事物进行动画处理,但同样:此代码有效,而不是实际问题。我希望你现在能更好地理解我的问题。

【问题讨论】:

  • 不幸的是,我没有时间编写任何示例代码,但 Apple 建议您使用 Auto-Layout / Constraints 系统,而不是硬编码特定数字(480.0f 或帧大小)还将隐含地为您的更改设置动画。当然,这仅在您面向 iOS 5+ 时才有用。
  • 好的,我认为会这样做,但我不知道如何做出正确的约束。我通过界面构建​​器尝试了一些东西,但它让我感到困惑,然后它有所帮助。

标签: iphone ios objective-c iphone-5


【解决方案1】:

使用下面的代码。

if([[UIScreen mainScreen] bounds].size.height == 568)
{
    //This is for iphone 5 view
    //add your code here.
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 150, 100, 100)];

}
else
{
    //This is for iphone 4 view
    //add your code here.
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
}

iphone 5 的这个条件。

【讨论】:

  • 分配新视图不起作用。它只是移回 iPhone 4 的位置,而不是移到新的 CGRectMake 位置。
【解决方案2】:

对于不同的ios,不需要comparision的高度: 试试这个来隐藏你的视图:

CGSize screenSize = [[UIScreen mainScreen] bounds].size;
[UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
[UIView setAnimationDuration:0.6];//in seconds
self.hiddenView.frame = CGRectMake(0, screenSize.height, 320, 260);//move the picker to a specific position
[UIView commitAnimations];//tells the compiler to start the animations

展示你的观点:

CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    [UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
    [UIView setAnimationDuration:0.6];//in seconds
    self.hiddenView.frame = CGRectMake(0, screenSize.height-260, 320, 260);//move the picker to a specific position
    [UIView commitAnimations];//tells the compiler to start the animations

更新 您可以使用bool 变量,也可以使用tapButton 的选定状态 喜欢:

-(IBAction) tapbuttonClicked:(UIButton *)sender
{
     [sender setSelected:!sender.isSelected];
     if(sender.isSelected)
      {
        //code to show your view...
      }
      else
      {
        //code to hide....
       }
}

【讨论】:

  • 谢谢你,但这实际上不是我的问题。请看更新。
【解决方案3】:

好的,因为显然没有人正确理解我的问题(或者没有真正读到最后?),我必须自己找到答案。如果有人遇到同样的问题,我就是这样解决的。

只需将您的代码放入-(void)viewDidLayoutSubviews{} 函数即可。它会在视图完全加载后立即执行。

   -(void)viewDidLayoutSubviews{
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;
        if (screenSize.height > 480.0f){
            [UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
            [UIView setAnimationDuration:0.0];//in seconds
            self.hiddenView.frame = CGRectMake(0, 504, 320, 260);//move the picker to a specific position
            [UIView commitAnimations];//tells the compiler to start the animations
        }
    }

【讨论】:

  • 感谢您试图弄清楚为什么我的视图不会移动 - 快疯了。
【解决方案4】:

我最喜欢的做法是使用+ (UIView) animateWithDuration: animations:

CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height > 480.0f){
[UIView animateWithDuration:0.60f animations:^(void){
self.hiddenView.frame = CGRectMake(0, 504, 320, 260);//move the picker to a specific position (504 = 568 - statusbar(20) - tapbar(44))
}];
}

这应该可以为您解决问题。另外,您可以查看animateWithDuration: animations: completion: 甚至animateWithDuration: delay: options: animations: completion:

希望这会有所帮助。

【讨论】:

  • 感谢您的回答,但这对我没有帮助。我想也许延迟元素会做到这一点,但事实并非如此。一秒钟后,视图刚刚移动到 iPhone 4 的位置。我定义的 CGRechtMake 无关紧要,它总是会移动到 iPhone 4 的位置。
【解决方案5】:

添加以下代码行:

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

yourProjectName-perfix.pch 文件

这对于识别设备是 iPhone4 或 iPhone5 很有用。通过给出类似的条件

if (IS_IPHONE_5)
{
    //// write code/setFrame for iPhone5;
}
eles
{
    //// write code/setFrame for iPhone4;
}

在项目的任何地方使用它:

【讨论】:

  • 谢谢你,但这实际上不是我的问题。请看更新。
猜你喜欢
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多