繁体   English   中英

如果iPhone是iPhone 5,请让UIView向下移动

[英]Make UIView move down if iPhone is iPhone 5

我当前正在创建一个在视图中包含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

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

使用下面的代码。

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。

无需comparision不同ios的高度:尝试以下操作以隐藏视图:

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选定状态, tapButton

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

好吧,因为显然没有人正确地理解我的问题(或者实际上没有读到最后吗?),所以我不得不为自己找到答案。 如果有人遇到相同的问题,这就是我的解决方法。

只需将代码放入-(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
        }
    }

添加以下代码行:

#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;
}

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

我最喜欢的方法是使用+ (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:

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM