gpt4 book ai didi

ios - 选择时的动画项目

转载 作者:行者123 更新时间:2023-11-29 02:14:59 25 4
gpt4 key购买 nike

我有一个 Collection View ,我想使图像的单元格更大,并在选择单元格时添加发光效果。我怎样才能做到这一点?

我尝试使用 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]];
}
];
}

最佳答案

如果您需要调整单元格的大小,您必须实现 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
}

关于ios - 选择时的动画项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28851982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com