【问题标题】:Slide UILabel Onto View Controller将 UILabel 滑到视图控制器上
【发布时间】:2013-05-18 20:53:19
【问题描述】:

我正在尝试以平滑的幻灯片类型动画从 UIViewController 顶部的起始位置移动 UILabel,以便在视图加载时它从顶部向下滑动,然后在 y 位置停止大约 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 位置。

【问题讨论】:

    标签: ios animation uilabel


    【解决方案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
                        }];
    });
    

    【讨论】:

      【解决方案2】:

      这样的事情应该可以工作:

      -(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
                                      }
                          ];
                      }
          ];
      }
      

      【讨论】:

      • 感谢您的回复。这行得通。唯一的问题是我的立场。它从 ViewController 顶部的左角 50.0f 向下移动,即从左上角移动到 ViewController 的中心。我希望它从顶部下降,然后垂直向上滑动而不从左到右移动。
      • 在动画开始之前,您需要确保标签的中心设置正确,而它仍然不在屏幕上。
      • 我添加了以下内容以尝试在动画开始之前设置位置,但似乎没有设置。例如lblInstruction.center = CGPointMake(lblInstruction.frame.origin.x, -20.0f); lblInstruction.bounds = CGRectMake(lblInstruction.frame.origin.x, -20.0f, 650.0f, 40.0f);但它仍然从左上角移动到新位置,然后回到左上角。我在 xib 上设置的确切位置是 -20.0f (x) 187.0f (y)。
      • 查看我的更新答案。我现在在动画开始之前设置标签的中心。
      • 设置中心似乎对我不起作用。动画仍然从左上角开始。我也尝试过设置 lblInstruction.center = CGPointMake(187.0f, -20.0f);这与 .xib 上的起始位置相同。
      【解决方案3】:

      如果它可以帮助其他人,这是有效的。我必须使用 .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-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多