Thursday, February 20, 2014

DraggingView Mac OS APP

when you want to use drag
(1)
-(void)_subscribe{   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_draggingConcludedHandler:)
                                                 name:DraggingConcludedNotification
                                               object:nil];
}

-(void)_unsubscribe{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)_draggingConcludedHandler:(NSNotification *)aNotification{
    DraggingView *aView = (DraggingView *)[aNotification object];
   
    for (IconImageView *childView in self.childViews){
        [childView setTargetImage:[aView targetImage]];
    }
}

(2)

#import <Cocoa/Cocoa.h>

/**
 * Drag&Drop detecting view fully convered on the main window's content view.
 */
@interface DraggingView : NSView{
    NSImage *_targetImage;
    BOOL _highlighted;
}
@property (nonatomic, retain) NSImage *targetImage;
@property (nonatomic, assign) BOOL highlighted;

@end

#import "DraggingView.h"

//Private
@interface DraggingView ()
-(void)_setup;
@end

@implementation DraggingView
@synthesize targetImage = _targetImage;
@synthesize highlighted = _highlighted;

-(void)awakeFromNib{
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSTIFFPboardType, nil]];
    [self _setup];
}

-(void)drawRect:(NSRect)dirtyRect{
    if (_highlighted) {
        [[NSColor blueColor] set];
        NSFrameRect([self bounds]);
    }
}

-(void)dealloc{
    self.targetImage = nil;
    [super dealloc];
}

#pragma mark -
#pragma mark Accessors

-(void)setHighlighted:(BOOL)yn{
    _highlighted = yn;
    [self setNeedsDisplay:YES];
}

#pragma mark -
#pragma mark Private

-(void)_setup{
    self.highlighted = NO;
}

@end

No comments:

Post a Comment