在 NSCollectionView 中顺序移动选择
Move selection sequentially in NSCollectionView
默认情况下,NSCollectionView 中的选择在一行(或一列)内通过箭头键移动。
如何使选择按顺序移动,就像按索引排列的项目一样?
Screenshot from developer.apple.com
我向 Apple 开发人员技术支持询问了同样的问题,在检查后他们说 "Our engineers have reviewed your request and have determined that this would be best handled as a bug report."。我向 Apple 的雷达提交了错误报告。至今没有回应。
我决定实施我自己的解决方案。子类化您的 NSCollectionView
并覆盖 keyDown 事件。
Swift 4
override func keyDown(with event: NSEvent) {
if event.modifierFlags.rawValue == 10617090 {
return
}
if event.isARepeat == true && event.keyCode != 123 && event.keyCode != 124 && event.keyCode != 125 && event.keyCode != 126 {
return
}
if event.keyCode == 123 || event.keyCode == 124 || event.keyCode == 125 || event.keyCode == 126 {
for index in self.selectionIndexes {
if event.keyCode == 124 && index < YOUR_DATASOURCE_ARRAY.count - 1 {
self.deselectItems(at: [NSIndexPath(forItem: index, inSection: 0) as IndexPath])
self.selectItems(at: [NSIndexPath(forItem: index + 1, inSection: 0) as IndexPath], scrollPosition: NSCollectionView.ScrollPosition.nearestHorizontalEdge)
return
}
if event.keyCode == 123 && index > 0 {
self.deselectItems(at: [NSIndexPath(forItem: index, inSection: 0) as IndexPath])
self.selectItems(at: [NSIndexPath(forItem: index - 1, inSection: 0) as IndexPath], scrollPosition: NSCollectionView.ScrollPosition.nearestHorizontalEdge)
return
}
}
}
super.keyDown(with: event)
}
这会中断左右箭头键输入并将选择移动到 previous/next 单元格。自从
"Allows Multiple Selection" 必须为 NO。同时禁用修改键,如 CMD 或 Control。
If you keep pressing right arrow, when selection arrives to last cell at right hand it jump one row down and keep moving or vice versa for left arrow.
希望能帮助到你。
默认情况下,NSCollectionView 中的选择在一行(或一列)内通过箭头键移动。
如何使选择按顺序移动,就像按索引排列的项目一样?
Screenshot from developer.apple.com
我向 Apple 开发人员技术支持询问了同样的问题,在检查后他们说 "Our engineers have reviewed your request and have determined that this would be best handled as a bug report."。我向 Apple 的雷达提交了错误报告。至今没有回应。
我决定实施我自己的解决方案。子类化您的 NSCollectionView
并覆盖 keyDown 事件。
Swift 4
override func keyDown(with event: NSEvent) {
if event.modifierFlags.rawValue == 10617090 {
return
}
if event.isARepeat == true && event.keyCode != 123 && event.keyCode != 124 && event.keyCode != 125 && event.keyCode != 126 {
return
}
if event.keyCode == 123 || event.keyCode == 124 || event.keyCode == 125 || event.keyCode == 126 {
for index in self.selectionIndexes {
if event.keyCode == 124 && index < YOUR_DATASOURCE_ARRAY.count - 1 {
self.deselectItems(at: [NSIndexPath(forItem: index, inSection: 0) as IndexPath])
self.selectItems(at: [NSIndexPath(forItem: index + 1, inSection: 0) as IndexPath], scrollPosition: NSCollectionView.ScrollPosition.nearestHorizontalEdge)
return
}
if event.keyCode == 123 && index > 0 {
self.deselectItems(at: [NSIndexPath(forItem: index, inSection: 0) as IndexPath])
self.selectItems(at: [NSIndexPath(forItem: index - 1, inSection: 0) as IndexPath], scrollPosition: NSCollectionView.ScrollPosition.nearestHorizontalEdge)
return
}
}
}
super.keyDown(with: event)
}
这会中断左右箭头键输入并将选择移动到 previous/next 单元格。自从 "Allows Multiple Selection" 必须为 NO。同时禁用修改键,如 CMD 或 Control。
If you keep pressing right arrow, when selection arrives to last cell at right hand it jump one row down and keep moving or vice versa for left arrow. 希望能帮助到你。