2011-12-12 35 views
1

有没有人将动画应用到AQGridViewCell s?我试图让每一个细胞,但第一个细胞在轻拍后消失。当应用CAAnimation时,AQGridView单元格会被换掉

问题是,当淡入开始时,单元格内容通常会被交换。例如,如果网格视图的第一行具有标签“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的已知缺陷,有这样的:

不要堆在彼此顶部多个动画。即 不会在网格视图上调用-beginUpdates,该网格视图的-isAnimatingUpdates 方法返回YES。不好的事情会发生,细胞将在错误的地方结束,堆积在另一个之上。

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

编辑

要看到,如果问题涉及到hidden财产,我反而动画细胞的不透明度(试验了两种方法描述here)。即使不透明度仅下降到0.5而不是0.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; 
} 
相关问题