0

我正在编写一个 Cocoa 应用程序。我写了一个可以拖放一个图像的代码。但现在我需要通过双击绘制几个图像并对其进行 D&D。每次双击-新图像,每次单击现有图像-启动 D&D。问题在于实现。我无法想象一种简单的实现方式。有人可以提出解决方案吗?

谢谢。

#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
4

1 回答 1

0

读:

可可绘图指南

查看 Cocoa 编程指南

Cocoa 的拖放编程主题

阅读这些内容后,请提出更有针对性的问题 - 这有点过于宽泛,无法简单回答。

于 2009-12-09T14:49:34.610 回答