Ike*_*aro 39 objective-c ios uicollectionview
我有一个UICollectionView,它由一个自定义的UICollectionViewCell子类组成.单元格正确显示,并通过触发此方法正确响应用户的触摸:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
但是,我理解当用户触摸单元格时,它应该突出显示(蓝色),然后当用户抬起手指时高光应该消失.这不会发生.有什么想法吗?
这是一些相关的代码:
在UICollectionView的数据源中:
@implementation SplitCheckViewCollection
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"ReceiptCellIdentifier";
SplitCheckCollectionCell *cell = (SplitCheckCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.cellName.text = [NSString stringWithFormat:@"%@%i",@"#",indexPath.row+1];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在UICollectionViewCell的实现中:
@implementation SplitCheckCollectionCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SplitCheckCollectionCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
SAE*_*SAE 33
该类仅告诉您高亮状态,但不会更改视觉外观.您必须通过例如更改单元格的背景以编程方式执行此操作.
" CollectionView编程指南"中介绍了详细信息.
Aja*_*arg 32
正如SAE所说,你必须在子类中自己完成.我遇到的另一个问题是,当点击一个单元格时,它正在接收突出显示并重新绘制,如果按下并保持单元格.但是,如果快速拍摄,重绘永远不会发生.
我在故事板中创建了单元格,并且集合视图将"延迟内容触摸"标记为默认值.我解开了这个,它立即显示手指触摸屏幕.
我正在使用自定义绘制例程来检查isHighlighted值.您还需要在自定义单元格中覆盖setHighlighted,如下所示,或者永远不会调用绘制例程.
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)
ash*_*rov 31
您应该实现两种委托方法:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)
使用动画突出显示集合视图单元格的完整代码:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor colorWithRed:232/255.0f green:232/255.0f blue:232/255.0f alpha:1]];
}
completion:nil];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor clearColor]];
}
completion:nil ];
}
Run Code Online (Sandbox Code Playgroud)
如果你想在触摸和释放触摸时有Highlight和Unhighlight效果,你需要实现UICollectionViewDataSource
这是示例代码
#pragma mark - UICollectionView Datasource
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:235/255.0f green:236/255.0f blue:237/255.0f alpha:.5];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = nil;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以通过将这些行添加到UICellView的设置来绘制一个hilight.
UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];
selectedBGView.backgroundColor = [UIColor redColor];
self.selectedBackgroundView = selectedBGView;
Run Code Online (Sandbox Code Playgroud)
从"管理选择和突出显示的可视状态"...集合视图默认支持单项选择,并且可以配置为支持多项选择或完全禁用选择.集合视图检测其边界内的点击并突出显示或相应地选择相应的单元格.在大多数情况下,集合视图仅修改单元格的属性以指示它已被选中或突出显示; 除了一个例外,它不会改变细胞的视觉外观.如果单元格的selectedBackgroundView属性包含有效视图,则集合视图会在突出显示或选择单元格时显示该视图.
小智 5
正如 SAE 指出的,您必须在突出显示的单元格中手动执行此操作。我发现的最简单的方法是使用 tableview didHighlightRowAtIndexPath 和 didUnhighlightRowAtIndexPath 方法,它们在 UICollectionCell 实例中设置 bool“突出显示”,然后在子类 UICollectionCell 类中覆盖该属性。这样做的美妙之处在于动画已经为您准备好了。您也可以在 UITableView/UITableViewCell 情况下执行相同的操作。
因此,在您的 UICollectionView 中使用 UICollectionViewDelegate 方法:
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.None)
}
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
collectionView.deselectItemAtIndexPath(indexPath, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
然后在您的 UICollectionViewCell 子类中添加以下内容:
override var highlighted:Bool{
didSet{
println("Highlighted is set \(highlighted)")
if(highlighted == true){
self.backgroundColor = UIColor.redColor()
}else{
self.backgroundColor = UIColor.blueColor()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51632 次 |
| 最近记录: |