Xcode 通过单击取消选择NSCollectionViewItem
如何通过再次单击取消选择NSCollectionViewItem 这是我用于选择和取消选择的代码:Xcode 通过单击取消选择NSCollectionViewItem,xcode,cocoa,flowlayout,nscollectionview,nscollectionviewitem,Xcode,Cocoa,Flowlayout,Nscollectionview,Nscollectionviewitem,如何通过再次单击取消选择NSCollectionViewItem 这是我用于选择和取消选择的代码: func collectionView(collectionView: NSCollectionView, didSelectItemsAtIndexPaths indexPaths: Set<NSIndexPath>) { print("selected") guard let indexPath = indexPaths.first else {re
func collectionView(collectionView: NSCollectionView, didSelectItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
print("selected")
guard let indexPath = indexPaths.first else {return}
print("selected 2")
guard let item = collectionView.itemAtIndexPath(indexPath) else {return}
print("selected 3")
(item as! CollectionViewItem).setHighlight(true)
}
func collectionView(collectionView: NSCollectionView, didDeselectItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
print("deselect")
guard let indexPath = indexPaths.first else {return}
print("deselect 2")
guard let item = collectionView.itemAtIndexPath(indexPath) else {return}
print("deselect 3")
(item as! CollectionViewItem).setHighlight(false)
}
/////////////////////
class CollectionViewItem: NSCollectionViewItem {
func setHighlight(selected: Bool) {
print("high")
view.layer?.borderWidth = selected ? 5.0 : 0.0
view.layer?.backgroundColor = selected ? NSColor.redColor().CGColor : NSColor(calibratedRed: 204.0/255, green: 207.0/255, blue: 1, alpha: 1).CGColor
}
}
func collectionView(collectionView:NSCollectionView,didSelectItemsAtIndexPaths indexPaths:Set){
打印(“选定”)
guard let indexPath=indexPaths.first-else{return}
打印(“选定2”)
guard let item=collectionView.itemAtIndexPath(indexPath)else{return}
打印(“选定3”)
(项目名称为!CollectionViewItem).setHighlight(true)
}
func collectionView(collectionView:NSCollectionView,DidDecelositemsatindExpaths indexPaths:Set){
打印(“取消选择”)
guard let indexPath=indexPaths.first-else{return}
打印(“取消选择2”)
guard let item=collectionView.itemAtIndexPath(indexPath)else{return}
打印(“取消选择3”)
(项目为!CollectionViewItem)。设置高亮显示(false)
}
/////////////////////
类CollectionViewItem:NSCollectionViewItem{
func设置高亮显示(所选:布尔){
打印(“高”)
view.layer?.borderWidth=选定?5.0:0.0
view.layer?.backgroundColor=选中?NSColor.redColor().CGColor:NSColor(已校准:204.0/255,绿色:207.0/255,蓝色:1,alpha:1)。CGColor
}
}
此代码在单击另一项时取消选择,但在单击同一项时不取消选择。我想在单击同一项时提供帮助。一个简单的技巧是使用CMD-鼠标左键单击。虽然这并不能完全解决我的问题,但总比什么都没有好。您可以通过观察项目上的选定状态,在项目被选中时在项目视图上安装一个
nsClickgestureRecognitizer
,并在取消选中时卸载它来实现这一点
将以下代码放在NSCollectionViewItem
子类中的某个位置:
- (void)onClick:(NSGestureRecognizer *)sender {
if (self.selected) {
//here you can deselect this specific item, this just deselects all
[self.collectionView deselectAll:nil];
}
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
[self installGestureRecognizer];
}
else {
[self uninstallGestureRecognizer];
}
}
- (void)installGestureRecognizer {
[self uninstallGestureRecognizer];
self.clickGestureRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self
action:@selector(onClick:)];
[self.view addGestureRecognizer:self.clickGestureRecognizer];
}
- (void)uninstallGestureRecognizer {
[self.view removeGestureRecognizer:self.clickGestureRecognizer];
self.clickGestureRecognizer = nil;
}