2013-05-18 57 views
1

我試圖從平滑幻燈片類型動畫中的UIViewController頂部的起始位置移動UILabel,以便在視圖加載後從頂部向下滑動,然後在ay位置約10秒。 10秒後,我想再次滑回視圖。將UILabel滑動到視圖控制器

-(void) animateInstructionLabel 
{ 
float newX = 50.0f; 
float newY = 100.0f; 

[UIView transitionWithView:self.lblInstruction 
        duration:10.0f 
        options:UIViewAnimationCurveEaseInOut 
       animations:^(void) { 
        lblInstruction.center = CGPointMake(newX, newY); 
       } 
       completion:^(BOOL finished) { 
        // Do nothing 
       }]; 
} 

但我不知道如何做10秒延遲,然後回到上面的方法。最終的結果是,我希望這是一個看起來像通知一樣的標籤,然後再移出屏幕。

有人能幫我把上述這些碎片放在一起嗎?

編輯

我加入這基於以下答案:

-(void) animateInstructionLabel 
{ 
float newX = lblInstruction.frame.origin.x; 
float newY = 20.0f; 

lblInstruction.center = CGPointMake(lblInstruction.frame.origin.x, -20.0f); 
lblInstruction.bounds = CGRectMake(lblInstruction.frame.origin.x, -20.0f, 650.0f, 40.0f); 

[UIView animateWithDuration:3.0f 
         delay:0 
        options:UIViewAnimationCurveEaseInOut 
       animations:^(void) { 
        lblInstruction.center = CGPointMake(newX, newY); 
       } 
       completion:^(BOOL finished) { 
        [UIView animateWithDuration:5.0f 
              delay:5.0 
             options:UIViewAnimationCurveEaseInOut 
             animations:^(void) { 
              lblInstruction.center = CGPointMake(newX, -20); 
             } 
             completion:^(BOOL finished) { 
              // Do nothing 
             } 
         ]; 
       } 
]; 
} 

但即使我的動畫開始我看到的是運動之前設置label.center關閉屏幕從viewcontroller的左上角到viewcontroller的中心。它不保留在動畫開始之前應該設置的x位置。

回答

0

這裏是什麼以防萬一它幫助別人。我必須使用.frame而不是.center來設置初始位置,然後將後續動畫設置回該位置。

-(void) animateInstructionLabel 
{ 

lblInstruction.frame = CGRectMake(lblInstruction.frame.origin.x, -40.0f, 650.0f, 40.0f); 
[self.view addSubview:lblInstruction]; 
lblInstruction.hidden = NO; 

float newX = lblInstruction.frame.origin.x; 
float newY = 20.0f; 

[UIView animateWithDuration:3.0f 
         delay:0 
        options:UIViewAnimationCurveEaseInOut 
       animations:^(void) { 
        lblInstruction.center = CGPointMake(newX, newY); 
       } 
       completion:^(BOOL finished) { 
        [UIView animateWithDuration:3.0f 
              delay:5.0 
             options:UIViewAnimationCurveEaseInOut 
             animations:^(void) { 
              lblInstruction.frame = CGRectMake(lblInstruction.frame.origin.x, -40.0f, 650.0f, 40.0f); 
             } 
             completion:^(BOOL finished) { 
              lblInstruction.hidden = YES; 
             } 
         ]; 
       } 
]; 
} 

這樣可以使標籤從視圖下滑下來,停下來5秒鐘,然後平滑地將其從視圖中垂直移回。

感謝以前的答案,讓我到上述最終的解決方案。

1

嘗試添加該動畫塊後(oldX,oldY是舊座標,動畫在標籤之前):

double delayInSeconds = 10.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [UIView transitionWithView:self.lblInstruction 
         duration:10.0f 
         options:UIViewAnimationCurveEaseInOut 
        animations:^(void) { 
         lblInstruction.center = CGPointMake(oldX, oldY); 
        } 
        completion:^(BOOL finished) { 
         // Do nothing 
        }]; 
}); 
1

像這樣的東西應該工作:

-(void) animateInstructionLabel { 
    float newX = 50.0f; 
    float newY = 100.0f; 

    labelInstruction.center = CGPointMake(newX, -20); // start off screen 

    [UIView animateWithDuration:10.0f 
       delay:0 
       options:UIViewAnimationCurveEaseInOut 
       animations:^(void) { 
        lblInstruction.center = CGPointMake(newX, newY); 
       } 
       completion:^(BOOL finished) { 
        [UIView animateWithDuration:10.0f 
           delay:10.0 
           options:UIViewAnimationCurveEaseInOut 
           animations:^(void) { 
            lblInstruction.center = CGPointMake(newX, -20); 
           } 
           completion:^(BOOL finished) { 
            // Do nothing 
           } 
        ]; 
       } 
    ]; 
} 
+0

感謝您的回覆。這工作。唯一的問題是我的立場。它從ViewController的頂部向左移動50.0f,即從左上角移動到ViewController的中心。我希望它從頂部下降,然後垂直向上滑動,而不會從左到右移動。 – motionpotion

+0

在動畫開始之前,您需要確保標籤的中心在屏幕尚未離開屏幕時正確設置。 – rmaddy

+0

我已經添加了以下內容來嘗試在動畫開始之前設置位置,但似乎沒有設置此位置。例如lblInstruction.center = CGPointMake(lblInstruction.frame.origin.x,-20.0f); lblInstruction.bounds = CGRectMake(lblInstruction.frame.origin.x,-20.0f,650.0f,40.0f);但它仍然從左上角移動到新位置,然後返回到左上角。我在xib上設置的確切位置是-20.0f(x)187.0f(y)。 – motionpotion

相關問題