2013-04-26 17 views
0

這淡入並通過按下一個按鈕,褪色的UIView,但目前看來,這個代碼可以寫更好:這是正確的方式淡入淡出UIView嗎?

- (IBAction)navigationTap:(id)sender { 
if (navigationFolded == TRUE) { 
    [UIView beginAnimations:@"MoveOut" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:0.2f]; 
    self.moveMe.frame = CGRectMake(0, 0, _moveMe.bounds.size.width, _moveMe.bounds.size.height); 
    [UIView commitAnimations]; 
    navigationFolded = FALSE; 
} else { 
    [UIView beginAnimations:@"MoveIn" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:0.2f]; 
    self.moveMe.frame = CGRectMake(50-_moveMe.bounds.size.width, 0, _moveMe.bounds.size.width, _moveMe.bounds.size.height); 
    [UIView commitAnimations]; 
    navigationFolded = TRUE; 
} 
+0

我想「淡入/淡出」應該是指改變視圖的透明度,而不是框架?你還應該使用UIView – Vladimir 2013-04-26 14:49:39

+0

的更新的基於塊的動畫方法,uiview從顯示器的左側淡入...... 你對「基於較新塊的動畫」有何意義? – greenchapter 2013-04-26 14:51:08

+0

如果它從左邊開始,那麼它「滑入」?無論如何,我不是母語爲英語的人,我可能是錯的。檢查[UIView animateWithDuration:...]方法和類似的 – Vladimir 2013-04-26 14:52:23

回答

0

你可以使用:

- (IBAction)navigationTap:(id)sender { 
if (navigationFolded == TRUE) { 
    [UIView beginAnimations:@"MoveOut" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:0.2f]; 
    self.moveMe.alpha = 1; 
    [UIView commitAnimations]; 
    navigationFolded = FALSE; 
} else { 
    [UIView beginAnimations:@"MoveIn" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
    [UIView setAnimationDuration:0.2f]; 
    self.moveMe.alpha = 0; 
    [UIView commitAnimations]; 
    navigationFolded = TRUE; 
} 

(也許是我的情況下混合起來 - 在這種情況下,IF語句應該使UIView可見,而其他的則會使它逐漸淡出YMMV)。

1

根據您的代碼,聽起來好像您希望UIView從屏幕的側面滑入。衰落將涉及動畫的UIView

你的代碼alpha值可以通過使用新的基於塊的語法做動畫,僅在if語句包裹的x位置的只是價值被簡化,因爲它是唯一的一部分更改爲navigationFolded

- (IBAction)navigationTap:(id)sender { 
    NSInteger xPos = 0; 
    if (!navigationFolded) { 
     xPos = 50-_moveMe.bounds.size.width 
    } 

    [UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
     self.moveMe.frame = CGRectMake(xPos, 0, _moveMe.bounds.size.width, _moveMe.bounds.size.height); 
    } completion:nil]; 

    navigationFolded = !navigationFolded; 
} 
+0

謝謝你這個代碼作品比我的版本短.. – greenchapter 2013-04-26 15:14:05

+1

我很高興它的工作。請將此問題標記爲已回答。 – 2013-04-26 15:26:10