将UILabel滑到View Controller上

时间:2013-05-18 20:53:20

标签: ios animation uilabel

我正在尝试将UILabel从UIViewController顶部的起始位置移动到平滑的滑动类型动画中,以便在视图加载时从顶部向下滑动,然后在yy位置停止约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设置在屏幕外,我看到它从viewcontroller的左上角移动到viewcontroller的中心。在动画开始之前,它不会保持应该设置的x位置。

3 个答案:

答案 0 :(得分: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 :(得分: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
                                }
                    ];
                }
    ];
}

答案 2 :(得分: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秒钟,然后从视图中垂直平滑地移回标签。

感谢您之前的答案,这些答案引导我完成上述最终解决方案。