当用户点击UICollectionView
时,我的UICollectionViewCell
会有一种奇怪的行为。
在我的假设中,根据apple docs,当用户点击UICollectionViewCell
时,单元格应该突出显示然后被选中。
但是在我的应用程序中,当用户点击单元格时,它只会突出显示而不会被选中。
当用户在单元格上滑动时,仅在这种情况下,单元格会被选中。
请帮忙。使用Xcode 6。
我在框中使用UICollectionView,使用自定义UICollectionViewCell类,它覆盖setSelected
和setHighlighted
。我已经实现了这些方法
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
但仅限于检查。
UPD:
我拍摄了视频http://take.ms/LzBkZ。
还提供代码:
**UICollectionViewController**
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"should");
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"highlighted");
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"select %@", indexPath);
_selectedCategory = _source[(NSUInteger) indexPath.row];
// _selectedNumber = [NSNumber numberWithInteger:category.id];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"deselect %@", indexPath);
if (_selectedCategory) {
_selectedCategory = nil;
}
}
并且
**SSCustomViewCell**
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
self.alpha = (CGFloat) (selected ? 0.4 : 1);
[self setNeedsDisplay];
}
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
self.alpha = (CGFloat) (highlighted ? 0.5 : 1);
[self setNeedsDisplay];
}
答案 0 :(得分:3)
UITapGesutureRecognizer
会阻止将触摸事件传播到UIView
默认。见the docs
您可以通过取消选中'取消视图中的触摸来禁用此功能。在IB或以下代码:
UITapGestureRecognizer *recognizer = self.myTapGestureRecognizer;
recognizer.cancelsTouchesInView = NO;