ios 如何自定义 UICollectionViewCell 子类的选择状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13657883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I customize the selection state of my UICollectionViewCell subclass?
提问by Mike Sprague
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.
我有一个自定义 UICollectionViewCell 子类,可以覆盖initWithFrame:
并layoutSubviews
设置其视图。但是,我现在正在尝试做两件我遇到麻烦的事情。
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
.
1)我正在尝试自定义UICollectionViewCell
选择时的状态。例如,我想改变图像的一个在UIImageView
在UICollectionViewCell
。
2) I want to animate (light bounce) the UIImage
in the UICollectionViewCell
.
2)我想UIImage
在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];
}
采纳答案by Jonathan
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:
.
将公共方法添加performSelectionAnimations
到MyCollectionViewCell
更改所需的定义UIImageView
并执行所需的动画。然后从collectionView:didSelectItemAtIndexPath:
.
So in MyCollectionViewCell.m:
所以在 MyCollectionViewCell.m 中:
- (void)performSelectionAnimations {
// Swap the UIImageView
...
// Light bounce animation
...
}
And in your UICollectionViewController
:
而在你的UICollectionViewController
:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell performSelectionAnimations];
}
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:
请注意,我已经取消了对 的调用[cell setSelected:YES]
,因为 UICollectionView 应该已经处理了该调用。从文档:
The preferred way to select the cell and highlight it is to use the selection methods of the collection view object.
选择单元格并突出显示它的首选方法是使用集合视图对象的选择方法。
回答by thomh
In your custom UICollectionViewCell subclass you could override the setSelected:
as so:
在您的自定义 UICollectionViewCell 子类中,您可以setSelected:
像这样覆盖:
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
[self animateSelection];
} else {
[self animateDeselection];
}
}
I have found that on repeated touches this method is called on a cell even if it's already selected so you may want to just check that you are really changing state before firing unwanted animations.
我发现在重复触摸时,即使已经选择了单元格,也会在单元格上调用此方法,因此您可能只想在触发不需要的动画之前检查您是否真的在更改状态。
回答by Mike Sprague
In your custom UICollectionViewCell
subclass, you can implement didSet
on the isSelected
property.
在您的自定义UICollectionViewCell
子类中,您可以didSet
在isSelected
属性上实现。
Swift 3:
斯威夫特 3:
override var isSelected: Bool {
didSet {
if isSelected {
// animate selection
} else {
// animate deselection
}
}
}
Swift 2:
斯威夫特 2:
override var selected: Bool {
didSet {
if self.selected {
// animate selection
} else {
// animate deselection
}
}
}
回答by Gaurav
If you want to show animation on selection then following method might helpful to you :
如果您想在选择时显示动画,那么以下方法可能对您有所帮助:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cell #%d was selected", indexPath.row);
// animate the cell user tapped on
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:0.8
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:UIColorFromRGB(0x05668d)];
}
completion:^(BOOL finished){
[cell setBackgroundColor:[UIColor clearColor]];
}
];
}
回答by Hardcoded
Should not mess with state when overridden in this way:
以这种方式覆盖时不应弄乱状态:
override var isSelected: Bool {
get {
return super.isSelected
}
set {
super.isSelected = newValue
.
.
.
}
}