【问题标题】:Show And Hide the UIView With Same Animation In Objective-C?在Objective-C中显示和隐藏具有相同动画的UIView?
【发布时间】:2018-01-09 09:14:44
【问题描述】:

我有一个UITextField 搜索框,起初它是隐藏的,我想用动画显示它,它可以工作。但是当我再次隐藏它时,动画突然发生,而不是动画中定义的时间。

这是我的代码:

    [UIView transitionWithView:searchbox duration:0.3 
    options:UIViewAnimationOptionTransitionCrossDissolve 
    animations:^{searchbox.hidden = NO;}completion:NULL];

    [UIView transitionWithView:searchbox duration:0.3 
    options:UIViewAnimationOptionTransitionCrossDissolve 
    animations:^{searchbox.hidden = YES;}completion:NULL];

【问题讨论】:

  • 只要增加隐藏的时间就可以看到效果。即使在您给定的时间,在我身边也能正常工作,但速度更快。
  • 这不是时间的问题,.hidden 方法在这种动画时间短的情况下无法正确,但 .alpha 可以做到。 @AadilAli

标签: ios objective-c xcode show-hide uiviewanimation


【解决方案1】:

使用视图的 alpha 属性而不是隐藏

[UIView transitionWithView:searchbox duration:0.3 
options:UIViewAnimationOptionTransitionCrossDissolve 
animations:^{searchbox.alpha = 1.0;}completion:NULL];

[UIView transitionWithView:searchbox duration:0.3 
options:UIViewAnimationOptionTransitionCrossDissolve 
animations:^{searchbox.alpha = 0;}completion:NULL];

【讨论】:

    【解决方案2】:

    显示

    searchbox.alpha = 0;
    searchbox.isHidden = NO;
    [UIView transitionWithView:searchbox
                 duration:0.3 
                 options:UIViewAnimationOptionTransitionCrossDissolve 
            animations:^{
              searchbox.alpha = 1.0
        }completion:NULL];
    

    隐藏

    searchbox.alpha = 1.0;
    searchbox.isHidden = NO;
    [UIView transitionWithView:searchbox
                 duration:0.3 
                 options:UIViewAnimationOptionTransitionCrossDissolve 
            animations:^{
              searchbox.alpha = 0
        }completion:^(isComplete){
          if (isComplete) {
             searchbox.isHidden = YES
          }}];
    

    【讨论】:

      【解决方案3】:

      动画调用之间应该有延迟。 这是一个例子:

      - (void)viewDidLoad {
          [super viewDidLoad];
          [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(show) userInfo:nil repeats:NO];
      }
      
      - (void) show {
          [UIView transitionWithView:searchbox duration:0.3
                             options:UIViewAnimationOptionTransitionCrossDissolve
                          animations:^{searchbox.hidden = NO;}completion:NULL];
      
          [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hide) userInfo:nil repeats:NO];
      }
      
      - (void) hide {
          [UIView transitionWithView:searchbox duration:0.3
                             options:UIViewAnimationOptionTransitionCrossDissolve
                          animations:^{searchbox.hidden = YES;}completion:NULL];
      }
      

      【讨论】:

      • 永远不要在 viewDidLoad 中做动画。永远不要试图猜测什么时候会发生。
      【解决方案4】:

      必须使用.alpha 隐藏或显示UIView

      正确的代码是:

      [UIView transitionWithView:searchbox duration:0.3 
      options:UIViewAnimationOptionTransitionCrossDissolve 
      animations:^{searchbox.alpha=1;}completion:NULL];
      
      [UIView transitionWithView:searchbox duration:0.3 
      options:UIViewAnimationOptionTransitionCrossDissolve 
      animations:^{searchbox.alpha=0;}completion:NULL];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 2021-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多