【问题标题】:UIView Animation not working in ios 5.1UIView 动画在 ios 5.1 中不起作用
【发布时间】:2013-02-15 10:19:31
【问题描述】:

我有一个UITableView。我想在表格中加载单元格时制作动画。我是这样弄的。

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect tmpFrame;
    CGRect originalFrame;
    originalFrame = cell.frame;
    tmpFrame = cell.frame;
    tmpFrame = CGRectMake(0, -50, 320, 50);
    cell.frame = tmpFrame;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.0f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 1)];
    separatorLineView.backgroundColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:separatorLineView];
    cell.frame = originalFrame;
    [UIView commitAnimations];
}

此代码在 iOS 6(iPhone 模拟器 6)中运行良好,但在 iOS 5.1(iPhone 模拟器 5.1)中无法运行。

【问题讨论】:

标签: iphone uitableview ios6 ios5.1


【解决方案1】:

完成。 我把这段代码放进去

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

在返回单元格之前。

【讨论】:

    【解决方案2】:

    所以我知道您已经有了解决方案,但我可能会建议您使用动画块,除非您要使其与旧版本兼容。无论如何,如果你不是,这可能会提供一种不同的方式来编写动画。我喜欢这样做,因为我觉得我对正在发生的事情有更多的控制权并且更少的混乱。

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]init];
    }
    
    CGRect movementFrame = cell.frame; // set the movement frame for modification
    movementFrame.origin.y -= 50; //adjust only one variable instead of making a rect..
    
    //Start your animation block. 
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         //Animation here
                         cell.frame = movementFrame; // Commit your changes basically
                     } completion:^(BOOL finished) {
                         //Completeion c
    
                     }];
    

    太棒了!保重^^

    编辑

    去测试它,并意识到我遗漏了一个关键部分。我很抱歉.. 但是这段代码确实有效,并且会在一个包含多个部分的分组表上制作这个动画,这很有趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2011-02-01
      相关资源
      最近更新 更多