0

这通过按下按钮淡入和淡入 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;
}
4

2 回答 2

1

根据您的代码,听起来您希望UIView从屏幕一侧滑入。褪色将涉及动画的alpha价值UIView

您的代码可以通过使用更新的基于块的动画语法来简化,并且仅将 x 位置的值包装在 if 语句中,因为它是唯一随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;
}
于 2013-04-26T15:05:03.513 回答
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 可见,而 else 会使它逐渐淡出。YMMV)。

于 2013-04-26T14:55:58.677 回答