选择时的动画项目
Animated item when selected
我有一个集合视图,我想使图像的单元格更大,并在选择单元格时添加发光效果。我该怎么做?
我尝试使用 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
方法。如果当前选择了单元格,您 return 不同的大小。最简单的方法是在 viewController 中创建一个 NSIndexPath @属性。然后在 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
}
我有一个集合视图,我想使图像的单元格更大,并在选择单元格时添加发光效果。我该怎么做?
我尝试使用 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
方法。如果当前选择了单元格,您 return 不同的大小。最简单的方法是在 viewController 中创建一个 NSIndexPath @属性。然后在 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
}