如何自定义UICollectionViewCell子类的选择状态? [英] How can I customize the selection state of my UICollectionViewCell subclass?

查看:207
本文介绍了如何自定义UICollectionViewCell子类的选择状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UICollectionViewCell子类,它会覆盖 initWithFrame: layoutSubviews 来设置其视图。但是,我现在正在尝试做两件我遇到问题的事情。

I have a custom UICollectionViewCell subclass that overwrites initWithFrame: and layoutSubviews to setup its views. However, I'm now trying to do two things which I'm having trouble with.

1)我正在尝试自定义<$ c $的状态c> UICollectionViewCell 选择后。例如,我想在 UICollectionViewCell 中的 UIImageView 中更改其中一个图像。

1) I'm trying to customize the state of the UICollectionViewCell upon selection. For example, I want to change one of the images in an UIImageView in the UICollectionViewCell.

2)我想在 UICollectionViewCell 中为 UIImage 设置动画(轻弹) 。

2) I want to animate (light bounce) the UIImage in the UICollectionViewCell.

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell setSelected:YES];
}


推荐答案

添加公共方法 performSelectionAnimations MyCollectionViewCell 的定义,它改变了所需的 UIImageView 并执行想要的动画。然后从 collectionView:didSelectItemAtIndexPath:调用它。

Add a public method performSelectionAnimations to the definition of MyCollectionViewCell that changes the desired UIImageView and performs the desired animation. Then call it from collectionView:didSelectItemAtIndexPath:.

所以在MyCollectionViewCell.m中:

So in MyCollectionViewCell.m:

- (void)performSelectionAnimations {
    // Swap the UIImageView
    ...

    // Light bounce animation
    ...
}

并在你的 UICollectionViewController

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell performSelectionAnimations];
}

注意我已经取消了对的调用[ cell setSelected:YES] ,因为它应该已由UICollectionView处理。从文档:

Notice I've taken out the call to [cell setSelected:YES], since that should already be taken care of by the UICollectionView. From the documentation:


选择单元格并突出显示它的首选方法是使用集合视图对象的选择方法。

The preferred way to select the cell and highlight it is to use the selection methods of the collection view object.

这篇关于如何自定义UICollectionViewCell子类的选择状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆