2009-12-09 2 views
0

J'écris une application Cocoa. J'ai écrit un code qui peut faire glisser & Drop une image. Mais maintenant j'ai besoin de dessiner plusieurs images en double-cliquant et D & D eux. Chaque double clic - nouvelle image, chaque clic sur l'image existante - commence D & D. Le problème est en cours de réalisation. Je ne peux pas imaginer un moyen simple de réalisation. Quelqu'un peut-il proposer une solution?Comment puis-je dessiner quelques images dans la vue personnalisée et glisser-déposer

Merci.

#import "DotView.h" 


@implementation DotView 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     center.x = center.y = 100.0; 
    } 
    return self; 
} 
- (void)drawRect:(NSRect)rect { 
     NSRect bounds = [self bounds]; 
     [[NSColor whiteColor] set]; 
     NSRectFill(bounds); 

     [[NSGraphicsContext currentContext] 
     setImageInterpolation: NSImageInterpolationHigh]; 

     NSSize viewSize = [self bounds].size; 
     NSSize imageSize = { 50, 40 }; 

     NSPoint imageOrigin = center; 
     imageOrigin.x -= imageSize.width * 0.50; 
     imageOrigin.y -= imageSize.height * 0.50; 


     NSRect destRect; 
     destRect.origin = imageOrigin; 
     destRect.size = imageSize; 

     NSString * file = @"/Users/classuser/Desktop/ded.jpg"; 
     NSImage * image = [[NSImage alloc] initWithContentsOfFile:file]; 
     [image setFlipped:NO]; 

     [image drawInRect: destRect 
       fromRect: NSZeroRect 
       operation: NSCompositeSourceOver 
       fraction: 1.0]; 

     NSBezierPath * path = [NSBezierPath bezierPathWithRect:destRect]; 
     } 
-(void)mouseDown:(NSEvent*)event 
{ 
    NSPoint point = [event locationInWindow]; 
     if(center.x<point.x+25 && center.x>point.x-25) 
      if(center.y<point.y+20 && center.y>point.y-20) 
       center = [self convertPoint:point fromView:nil]; 
} 

- (void) mouseDragged:(NSEvent*)event { 

    [self mouseDown:event]; 
    [self setNeedsDisplay:YES]; 

} 


@end 

Répondre

Questions connexes