簡體   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