2

我在 collectionview 中显示了一些图像。现在我想选择图像并按空格键。如果我按下空格按钮,图像应该在单独的窗口中快速查看。任何想法?

4

2 回答 2

4

在您看来,请执行以下操作:

- (void)keyDown:(NSEvent *)event
{
  unichar firstChar = 0;
  if ([[event charactersIgnoringModifiers] length] > 0)
    firstChar = [[event charactersIgnoringModifiers] characterAtIndex:0];

  if (firstChar == ' ')
  {
    if ([QLPreviewPanel sharedPreviewPanelExists]
        && [[QLPreviewPanel sharedPreviewPanel] isVisible])
    {
      [[QLPreviewPanel sharedPreviewPanel] orderOut:nil];
    }
    else
    {
      [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];
      [[NSApp mainWindow] makeKeyWindow];
    }
  }
  else if (firstChar == NSRightArrowFunctionKey)
  {
    if ([QLPreviewPanel sharedPreviewPanelExists]
        && [[QLPreviewPanel sharedPreviewPanel] isVisible])
    {
      [[QLPreviewPanel sharedPreviewPanel] selectNextItem];
      return;
    }
  }
  else if (firstChar == NSLeftArrowFunctionKey)
  {
    if ([QLPreviewPanel sharedPreviewPanelExists]
        && [[QLPreviewPanel sharedPreviewPanel] isVisible])
    {
      [[QLPreviewPanel sharedPreviewPanel] selectPreviousItem];
      return;
    }
  }
  else
    [super keyDown:event];
}

然后,我在我的应用程序委托 (AppDelegate.m) 中执行此操作:

- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel
{
  //note that this methods indeed gets called because NSApp's
  //delegate is in the responder chain.
  return YES;
}

- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel
{
  previewPanel = panel; //set an ivar
  [panel setDataSource:self];
}

- (void)endPreviewPanelControl:(QLPreviewPanel *)panel
{
  previewPanel = nil;
}

- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel
{
  //return a number of your choice (depends on your own app)
}

- (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel
                previewItemAtIndex:(NSInteger)index
{
  //return an object of your choice (depends on your app)
}

- (void)handleCurrentFileItemsSelectionChange:(NSNotification *)note
{
  [previewPanel reloadData]; //referring to the ivar
}
于 2013-05-25T11:27:44.437 回答
1

我终于明白了。我刚刚替换了 imageViewes 并将 NSButton 并将空格按钮设置为 keyequivalent

- (BOOL)isOneOfMyKeyEquivs:(NSString *)keyChar
{
    if (([keyChar isEqualToString:@" "])||([keyChar isEqualToString:@"\r"])) {
        return YES;
    } else {
        return NO;
    }
}
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
{

        //NSLog(@"OK");
        NSString* keyChar = [theEvent characters];
        if ([self isOneOfMyKeyEquivs:keyChar])
        {
            [[self cell] setKeyEquivalent:keyChar];
        }
        return [super performKeyEquivalent:theEvent];


    return NO;
}

并执行按钮操作。

于 2013-06-01T10:41:22.657 回答