【问题标题】:Animate UITableViewCells with repeat and scroll使用重复和滚动动画 UITableViewCells
【发布时间】:2015-03-18 04:43:58
【问题描述】:

我有一个简单的表格视图,我正在使用带有一个 UIImage 和一些标签的自定义单元格。

我想为图像大小设置动画,为此我在cellForRowAtIndexPath 方法中添加了以下内容:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"customCell";

        NotificationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[NotificationTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

if( [[[notification objectAtIndex:[indexPath section]] valueForKey:@"readed"] isEqualToString:@"readed"])
    {   

     [UIView animateWithDuration:0.9
                              delay:0.3
                            options:UIViewAnimationOptionBeginFromCurrentState |  UIViewAnimationOptionRepeat
                         animations:(void (^)(void)) ^{
                             cell.imgIcon.transform=CGAffineTransformMakeScale(0.8, 0.8);
                         }
                         completion:^(BOOL finished){
                             cell.imgIcon.transform=CGAffineTransformIdentity;
                         }];
    }
    return cell;
    }

当我启动应用程序时,它运行正常,只有“已读取”的单元格在带有 UIViewAnimationOptionRepeat 选项的 bucle 中进行动画处理。

当我滚动表格视图时,问题就开始了。一些“读取”的单元格停止动画,并且每个单元格的单元格动画时间循环不同。

有什么想法吗?

谢谢

已编辑:我认为问题出在可重复使用的单元格上。

【问题讨论】:

    标签: ios objective-c uitableview animation tableviewcell


    【解决方案1】:

    您说“我尝试重新加载 tableview 但这不起作用。” (原文如此)

    您尝试过何时以及如何重新加载表格视图?为了回应一个 UIApplicationWillEnterForegroundNotification 消息处理程序被调用?发布该代码。

    我会在你的 cellForRowAtIndexPath 方法中添加一条日志语句,以确保它被调用,并记录正在更新的 indexPath(s)。

    您确定希望每次应用返回前台时,表格视图的每个单元格中的图像都具有动画效果吗?这似乎很烦人。

    【讨论】:

    • 如果没有阅读通知,我真的只是在扔动画。
    • 这根本没有澄清你的问题。
    • 当应用激活时,我正在重新加载 tableView。[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    • 方法 cellForRowAtIndexPath 被正确调用。
    【解决方案2】:

    正如邓肯所说,重新加载 tableView 应该会导致动画发生。确保您断点/记录动画以确保代码实际到达那里。一旦你弄清楚如何通过重新加载表格使动画再次出现,你可以添加两个 NSNotificationCenter 标注,让应用知道一旦应用从后台恢复活动就重新加载表格。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
    

    只需设置适当的选择器来处理和重新加载您的表格:

    例如

    - (void)appDidBecomeActive:(id)sender {
        [_tableView reloadData];
    }
    

    【讨论】:

    • 这就是我正在尝试的。当应用程序处于活动状态时,我会抛出 [_tableView reloadData]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    相关资源
    最近更新 更多