应用CAAnimation时,AQGridView单元格会被交换

时间:2011-12-12 15:49:44

标签: iphone ios ipad caanimation aqgridview

是否有人成功将动画应用于AQGridViewCell?我试图制作每个细胞但第一个细胞在敲击后消失。

问题是,当淡入淡出开始时,单元格内容通常会被换掉。例如,如果网格视图的第一行具有标签为“1”,“2”,“3”的单元格,则标签可能会交换为“1”,“3”,“2”。

- (AQGridViewCell *)gridView:(AQGridView *)aGridView cellForItemAtIndex:(NSUInteger)index
{
    static NSString *CellIdentifier = @"ReusableGridViewCell";

    AQGridViewCell *cell = (AQGridViewCell *)[gridView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"ReusableGridViewCell" owner:self options:nil];

        cell = [[[AQGridViewCell alloc] initWithFrame:gridViewCellContent.frame
                                      reuseIdentifier:CellIdentifier] autorelease];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [cell.contentView addSubview:label];
        [label release];

        cell.selectionStyle = AQGridViewCellSelectionStyleNone;
    }

    UILabel *label = [[cell.contentView subviews] objectAtIndex:0];
    if (! tapped)
    {
        label.text = [NSString stringWithFormat:@"%u", index];
    }   
    else if (index > 0)
    {
        CATransition *cellAnimation = [CATransition animation];
        cellAnimation.duration = 3.0;
        cellAnimation.type = kCATransitionFade;
        cellAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        [label.layer addAnimation:cellAnimation forKey:kCATransition];              // labels get swapped
//      [cell.contentView.layer addAnimation:cellAnimation forKey:kCATransition];   // labels get swapped
//      [cell.layer addAnimation:cellAnimation forKey:kCATransition];               // labels get swapped, one cell immediately disappears
        NSLog(@"%u", [gridView isAnimatingUpdates]);                                // prints "0"

        label.hidden = YES;
    }

    return cell;
}

- (void)gridView:(AQGridView *)aGridView didSelectItemAtIndex:(NSUInteger)index
{
    tapped = YES;
    [gridView reloadData];
}

我尝试在一堆AQGridViewAQGridViewCell等方法中设置断点,以尝试找到导致交换的断点。找不到它。

在已知的AQGridView错误中,有以下内容:

  

不要试图将多个动画堆叠在一起。即   不要在-isAnimatingUpdates的网格视图上调用-beginUpdates   方法返回YES。糟糕的事情会发生,细胞最终将会出现   错误的地方,堆叠在一起。

在上面的代码中,-isAnimatingUpdates返回NO。即便如此,也许我所看到的是AQGridView中的另一个相关错误 - 我将提交错误报告。但是,由于我的情况非常简单,我想知道是否有人之前遇到过这种情况并想出一个解决方法,也许某种方式可以关闭AQGridView内部的动画。

修改

要查看问题是否与hidden属性相关,我改为为单元格的不透明度设置了动画(尝试了两种方法here)。即使不透明度仅下降到0.5而不是0.0,细胞仍然在交换。

1 个答案:

答案 0 :(得分:1)

这是一种解决方法。如果有人找到更优雅的解决方案,我会将其标记为答案。

- (void)gridView:(AQGridView *)aGridView didSelectItemAtIndex:(NSUInteger)index
{
    // Create a copy of the 0th cell's content and display it on top of that cell.
    AQGridViewCell *selectedCell = [aGridView cellForItemAtIndex:0];
    UILabel *selectedLabel = [[selectedCell.contentView subviews] objectAtIndex:0];
    CGRect frame = [self.view convertRect:selectedLabel.frame fromView:selectedLabel.superview];
    UILabel *labelOnTop = [[UILabel alloc] initWithFrame:frame];
    labelOnTop.text = selectedLabel.text;
    [self.view addSubview:labelOnTop];
    [labelOnTop release];

    // Fade away the grid view as a whole (not individual cells). 
    CATransition *cellAnimation = [CATransition animation];
    cellAnimation.duration = 3.0;
    cellAnimation.type = kCATransitionFade;
    cellAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [aGridView.layer addAnimation:cellAnimation forKey:kCATransition];
    aGridView.hidden = YES;
}