2014-04-23 30 views
0

我正在做一個簡單的調用viewDidApepar中的動畫的方法,在我的應用程序中,動畫在故事板中的視圖的初始位置是(10,5) - 幾乎隱藏主視圖。動畫在視圖中的奇怪行爲DIDAppear

-(void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    [self doTheAnimation];  
} 

和方法包含

[UIView animateWithDuration:0.8f delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:1.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
    [plotView setFrame:CGRectMake(10, 40, 299, 130)]; 
} completion:nil]; 

問題:當應用程序被啓動 - 所有的偉大工程具有清涼「春天」的效果,但是當我推到另外的看法,回去在主視圖中,調用動畫的方法被調用,LOG顯示plotView已將frame.origin更改爲(10,40),但它仍停留在storyBoard上的初始位置(10,5)處。

+0

所以你想只運行一次動畫? – Desdenova

+0

嘗試從ViewWillAppear調用該方法。 –

+0

@Desdenova - 每當主視圖呈現時,所以每次viewAppears – EmilDo

回答

1

更換

-(void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    [self doTheAnimation];  
} 

-(void) viewDidLayoutSubviews{ 
    [super viewDidLayoutSubviews]; 
    [self doTheAnimation];  
} 
+0

這樣做很好,我應該檢查蘋果開發頁面以獲得關於此方法的更多信息。 – EmilDo

1

不要改變你的方法的地方,只是補充一點:

[plotView setFrame:CGRectMake(10, 5, 299, 130)]; 
[UIView animateWithDuration:0.8f delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:1.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
    [plotView setFrame:CGRectMake(10, 40, 299, 130)]; 
} completion:^{ 
    [plotView setFrame:CGRectMake(10, 40, 299, 130)]; 
}]; 

告訴我,如果它的工作原理:)

0

更好的是:

[self performSelector:@selector(doTheAnimation) withObject:nil afterDelay:0.5]; 

根據需要設置延遲。