gpt4 book ai didi

macos - 单击 osx 10.6 选择 View

转载 作者:行者123 更新时间:2023-12-03 17:12:53 25 4
gpt4 key购买 nike

我创建了一个 ImageView

for(int i=0; i<pcount; i++)
{
int x = rand() % 350;
int y = rand() % 350;
NSRect rect = NSMakeRect((x+10),(y+10), 200, 200);
//NSImageView *imageView
imageView1 = [[NSImageView alloc]initWithFrame:rect];
[imageView1 setTag:i];

// imageView = [[NSImageView alloc]initWithFrame:rect];
// [imageView1 rotateByAngle:rand() % 150];

[imageView1 setImageScaling:NSScaleToFit];
[imageView1 canBecomeKeyView];
NSImage *theImage = [[NSImage alloc]initWithContentsOfURL:(NSURL*)[patharray objectAtIndex:(i)]];
[imageView1 setImage:theImage];
[[imageView1 cell] setHighlighted:YES];
[[layoutCustom view] addSubview:imageView1 positioned:NSWindowMovedEventType relativeTo:nil];}

现在如何通过鼠标单击来选择每个 ImageView ?提前致谢。

最佳答案

我在这里假设您有不使用现有 Collection View 的原因。因此,从我在您的代码中读到的内容来看,您有layoutCustom.view,其中包含一堆 NSImageView。这里有两个选项:

  1. 在您的layoutCustom对象中实现mouseDown:(或mouseUp:或两者)。将事件位置转换为 View 坐标并查找 CGRectContainsPoint(subview.frame, mouseDownPoint) 返回 YES 的任何 subview 。您应该选择该 View 。

  2. 子类化 NSImageView 并实现 mouseDown:(或 mouseUp: 或两者)。在 mouseDown 上:只需设置一个“选定”标志即可。 View 可以在选择时自行绘制某些内容,或者layoutCustom对象可以观察属性并相应地绘制选择。

我更喜欢选项 1,因为它更简单,需要更少的类以及对象之间的交互更少。

// Option 1 (in layoutCustom class)

- (void) mouseDown:(NSEvent*)theEvent {
CGPoint mouseDownPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
for (NSView *view in self.subviews) {
if (CGRectContainsPoint(view.frame, mouseDownPoint)) {
// Do something to remember the selection.
// Draw the selection in drawRect:
[self setNeedsDisplay:YES];
}
}
}




// Option 2 (in Custom subclass of NSImage)

- (void) mouseDown:(NSEvent*)theEvent {
self.selected = !self.selected;
}


// Option 2 (in layoutCustom class)
- (void) addSubview:(NSView*)view positioned:(NSWindowOrderingMode)place relativeTo:(NSView*)otherView {
[super addSubview:view positioned:place relativeTo:otherView];
[self startObservingSubview:view];
}

- (void) willRemoveSubview:(NSView*)view {
[self stopObservingSubview:view];
}

- (void) startObservingSubview:(NSView*)view {
// Register your KVO here
// You MUST implement observeValueForKeyPath:ofObject:change:context:
}

- (void) stopObservingSubview:(NSView*)view {
// Remove your KVO here
}

关于macos - 单击 osx 10.6 选择 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15917121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com