选中动画项目

时间:2015-03-04 10:24:24

标签: ios objective-c uicollectionview

我有一个集合视图,我想让图像的单元格更大,并在选择单元格时添加发光效果。我怎么能这样做?

我正在尝试使用didSelectItemAtIndexPath,但它无法正常工作。

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        // animate the cell user tapped on
        UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];
        [UIView animateWithDuration:5.0
                              delay:0
                            options:(UIViewAnimationOptionAllowUserInteraction)
                         animations:^{
                             NSLog(@"animation start");
                             [cell setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0]];
                             cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width+20, cell.frame.size.height+20);
  recipeImageView.frame=cell.frame;

 NSString* resourcePath = [soundUrl objectAtIndex:indexPath.row];
                             NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
                             NSError *error;

                             _audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
                             _audioPlayer.numberOfLoops = 0;
                             _audioPlayer.volume = 1.0f;
                             [_audioPlayer prepareToPlay];

                             if (_audioPlayer == nil)
                                 NSLog(@"%@", [error description]);
                             else
                                 [_audioPlayer play];
                         }
                         completion:^(BOOL finished){
                             NSLog(@"animation end");
                             recipeImageView.frame=cell.frame;

                             //[cell setBackgroundColor:[UIColor whiteColor]];
                         }
         ];
    }

1 个答案:

答案 0 :(得分:1)

如果您需要调整单元格的大小,则必须实现UICollectionViewDelegateFlowLayout方法。如果当前选择了单元格,则返回不同的大小。最简单的方法是在viewController中创建一个NSIndexPath @property。然后在tableView:didSelectItemAtIndexPath:中更改indexPath并调用performBatchUpdates:completion:,这将重新加载单元格的大小。

代码很简单

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.selectedIndexPath isEqual:indexPath]) {
        return CGSizeMake(200, 200);
    }
    else {
        return CGSizeMake(200, 100);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // animate the cell user tapped on
    if ([self.selectedIndexPath isEqual:indexPath]) {
        // deselect if same cell was tapped
        self.selectedIndexPath = nil;
    }
    else {
        // select new cell
        self.selectedIndexPath = indexPath;
    }
    // do something. e.g. change colors, start player

    [collectionView performBatchUpdates:nil completion:nil]; // will trigger size changes
}