user23790
user23790

Reputation: 563

select a view by clicking osx 10.6

i created a image view

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];}    

now how can select each image view by mouse click ? thanks in advance.

Upvotes: 1

Views: 238

Answers (2)

aLevelOfIndirection
aLevelOfIndirection

Reputation: 3522

I'm assuming here that you have your reasons for not using existing collection views. So from what I read in your code you have layoutCustom.view, which contains a bunch of NSImageViews. Here are two options:

  1. In your layoutCustom object implement the mouseDown: (or mouseUp: or both). Take the event location convert it view coordinates and look for any subview for which CGRectContainsPoint(subview.frame, mouseDownPoint) return YES. You should select that view.

  2. Subclass NSImageView and implement mouseDown: (or mouseUp: or both). On mouseDown: simply set a "selected" flag. Either the view can draw something itself when selected or the layoutCustom object can observe the property and draw the selection accordingly.

I would prefer option 1 because it simpler, requires fewer classes and fewer interactions between objects.

// 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
}

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

I've got a better idea: Instead of fighting with converting mouse clicks in a view to coordinates and then figuring out how to map it to the right subview or sub-image, why not have one big (or scrolling?) view and then add your images as giant "NSButton" objects (set to custom type), where the button images can be the images you want to add.

As for how to select each image? You can either subclass "NSButton" and keep track of some custom data within it, or you can use a "tag" to figure out which button was pressed in your "IBAction" method and then decide what to do with it.

Another approach might be to embed your images into NSTableView cells...

Upvotes: 0

Related Questions