1

我正在尝试以平滑的幻灯片类型动画从 UIViewController 顶部的起始位置移动 UILabel,以便在视图加载时它从顶部向下滑动,然后在任何位置停止大约 10 秒。10 秒后,我想再次滑出视图。

-(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 设置在屏幕之外,我还是看到它从视图控制器的左上角移动到了视图控制器的中心。它没有保持动画开始之前应该设置的 x 位置。

4

3 回答 3

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
                    }];
});
于 2013-05-18T21:04:23.610 回答
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
                                }
                    ];
                }
    ];
}
于 2013-05-18T21:04:54.097 回答
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 秒钟,然后将其平滑地垂直移回视图之外。

感谢之前的答案,这些答案使我得到了上述最终解决方案。

于 2013-05-20T11:22:43.950 回答