1

有没有人成功地将动画应用于AQGridViewCells?我试图让每个单元格,但第一个单元格在点击后消失。

问题是当淡入淡出开始时,单元格内容通常会被交换。例如,如果网格视图的第一行包含标签为“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 方法返回YES 的网格视图上调用-beginUpdates。坏事会发生,细胞最终会出现在错误的地方,彼此堆叠在一起。

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

编辑

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

4

1 回答 1

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;
}
于 2011-12-12T16:13:40.017 回答