【问题标题】:How to add animation when the view becomes visible?当视图可见时如何添加动画?
【发布时间】:2015-04-17 05:04:26
【问题描述】:

我在静态UITableViewCell 中有一个UIViewview 设置为隐藏在 viewDidLoad 中。当button 被选中时,view 就会可见。

- (void)viewDidLoad {
    [self.myView setHidden:YES];
}

- (IBAction)myButton:(id)sender
{
    if (self.myView.hidden == YES) {
        [self.myView setHidden:NO];
    } else {
        [self.myView setHidden:YES];
    }

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        if (self.myView.hidden == YES) {
            retuen 252 - self.myView.bounds.size.height;
        } else return 252;
    }
    tableView.estimatedRowHeight = 150;
    return UITableViewAutomaticDimention;
}

现在发生的情况是,当button 被选中时,view 变得可见并且cells 的高度会随着动画而改变;但是view 会立即可见,并且会妨碍其他对象(直到cells 动画结束)。

我怎样才能让view 也输入动画? (我更喜欢它的高度动画。)

编辑

我尝试了以下动画高度,以便它应该与cell同步

if (self.myView.hidden == YES) {
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y - self.myView.frame.size.height, self.myView.frame.size.width, self.myView.frame.size.height)];

    [self.myView setHidden:NO];
    [UIView animateWithDuration:1.0 animations:^(){
        [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
    }];
}

但它所做的只是动画views 位置,(从上到下,)而不是它的高度??

【问题讨论】:

    标签: ios objective-c uitableview animation hidden


    【解决方案1】:

    你可以这样改,

    if (self.myView.hidden == YES) {
        [self.myView setAlpha:0.0];
        [self.myView setHidden:NO];
    }
    [UIView animateWithDuration:0.2 animations:^{
        if (self.myView.hidden == YES) {
             [self.myView setAlpha:1.0];
        } else {
             [self.myView setAlpha:0.0];
        }
    }
    completion:^(BOOL finished) {
        if (self.myView.alpha == 0.0) {
            [self.myView setHidden:YES];
        }
    }];
    

    希望对您有所帮助。

    享受编码!

    【讨论】:

    • 这是因为您更改了self.myviewy 坐标。您可以按原样设置y,并根据需要增加高度。
    【解决方案2】:

    在 myButton: 内你可以使用的事件

    - (IBAction)myButton:(id)sender
    {
    
        if (self.myView.hidden == YES) {
            [self.myView setHidden:NO];
        } else {
            [self.myView setHidden:YES];
        }
    
       [self.myView setAlpha:0.0f];
       [UIView animateWithDuration:1.0
                              delay:0.0
                            options:nil
                         animations:^{
                             [self.myView setAlpha:1.0f];
                         }
                         completion:^(BOOL complete){
                             [self.myView setAlpha:1.0f];
                         }];
    
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    } 
    

    如果你想为视图的高度设置动画,你可以这样做-

    CGFloat viewHeight = self.myView.frame.size.height;
        [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, 0)];
               [UIView animateWithDuration:1.0
                                      delay:0.0
                                    options:nil
                                 animations:^{
                                     [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, viewHeight)];
                                 }
                                 completion:^(BOOL complete){
                                     [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, viewHeight)];
                                 }];
    

    【讨论】:

    • 刚刚试过了,它没有动画。 (顺便说一句,我用的是UIDatePicker
    • 你把viewHeight初始化为CGFloat viewHeight = self.myView.frame.size.height;
    【解决方案3】:

    UIView 上的 hidden 属性不可动画化,您需要取消隐藏它,然后将其设置为动画即将开始时应处于的状态(因此可能设置 alpha = 0 或 height = 0 ) 然后在动画块中,将其设置为您希望它处于的正确状态 (alpha = 1, height = cell.frame.size.height)

    例如

    view.hidden = NO;
    view.alpha = 0;
    
    [UIView animateWithDuration:0.25 animations:^() {
        view.alpha = 1;
    }];
    

    【讨论】:

    • 关于您的编辑,您只是更改视图框架的 y 位置而不是高度,CGRectMake 的最后一个参数是高度
    【解决方案4】:

    //显示

       [UIView animateWithDuration:0.7f
                                  delay:0
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{
                                 viewtemp.alpha = 0.912f;
                             } completion:NULL];
    

    //隐藏

     [UIView animateWithDuration:0.9f
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             viewtemp.alpha = 0.0f;
                         } completion:NULL];
    

    【讨论】:

      【解决方案5】:

      @arnold,您尝试更改其Y Position,而不是height,您需要在其Height 上应用动画。如下所示。

      if (self.myView.hidden == YES) {
      [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, 0)];
      
      [self.myView setHidden:NO];
      [UIView animateWithDuration:1.0 animations:^(){
          [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
      }];
      }
      

      【讨论】:

      • @Arnold,你能检查一下UIView's 的高度是多少吗?
      猜你喜欢
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多