移动NSView直到它到达边框

时间:2011-04-21 13:35:23

标签: objective-c cocoa draggable nsview

在一个基于Cocoa的应用程序中,我有一个用于绘图的画布,从NSView继承,以及一个矩形,也从NSView继承。拖动画布内部的矩形是没有问题的:

-(void)mouseDragged:(NSEvent *)theEvent {
  NSPoint myOrigin = self.frame.origin;

  [self setFrameOrigin:NSMakePoint(myOrigin.x + [theEvent deltaX], 
                                   myOrigin.y - [theEvent deltaY])];
}

像魅力一样工作。我现在遇到的问题:如何防止矩形移动到画布外?

所以,首先我想解决这个问题,仅针对左边框,然后调整其他边缘。我的第一个想法是:“检查矩形的x原点是否为负”。但是:一旦它是负的,矩形就不能再被移动(自然)。我通过在else-branch中将矩形移动到零x偏移来解决这个问题。这有效,但它很......丑陋。

所以我对这一点感到困惑,任何暗示?绝对是解决方案非常接近和简单。这很容易,我无法弄明白(一如既往的简单解决方案;)。

此致

的Mac

1 个答案:

答案 0 :(得分:5)

我建议不要使用deltaXdeltaY;尝试在 superview 中使用活动的位置。您需要参考子视图。

// In the superview
- (void)mouseDragged:(NSEvent *)event {
    NSPoint mousePoint = [self convertPoint:[event locationInWindow] 
                             fromView:nil];

    // Could also add the width of the moving rectangle to this check 
    // to keep any part of it from going outside the superview
    mousePoint.x = MAX(0, MIN(mousePoint.x, self.bounds.size.width));
    mousePoint.y = MAX(0, MIN(mousePoint.y, self.bounds.size.height));

    // position is a custom ivar that indicates the center of the object;
    // you could also use frame.origin, but it looks nicer if objects are 
    // dragged from their centers
    myMovingRectangle.position = mousePoint;
    [self setNeedsDisplay:YES];
}

你在mouseUp:进行基本相同的边界检查。

更新:您还应该查看“查看编程指南”,该指南将引导您创建可拖动的视图:Creating a Custom View


示例代码应该有用,但与原始问题并不严格相关:

在DotView.m中:

- (void)drawRect:(NSRect)dirtyRect {
    // Ignoring dirtyRect for simplicity
    [[NSColor colorWithDeviceRed:0.85 green:0.8 blue:0.8 alpha:1] set];
    NSRectFill([self bounds]);
    // Dot is the custom shape class that can draw itself; see below
    // dots is an NSMutableArray containing the shapes
    for (Dot *dot in dots) {
        [dot draw];
    }
}

- (void)mouseDown:(NSEvent *)event {
    NSPoint mousePoint = [self convertPoint:[event locationInWindow] 
                             fromView:nil];

    currMovingDot = [self clickedDotForPoint:mousePoint];
    // Move the dot to the point to indicate that the user has
    // successfully "grabbed" it    
    if( currMovingDot ) currMovingDot.position = mousePoint;
    [self setNeedsDisplay:YES];
}

// -mouseDragged: already defined earlier in post

- (void)mouseUp:(NSEvent *)event {
    if( !currMovingDot ) return;

    NSPoint mousePoint = [self convertPoint:[event locationInWindow] 
                             fromView:nil];
    spot.x = MAX(0, MIN(mousePoint.x, self.bounds.size.width));
    spot.y = MAX(0, MIN(mousePoint.y, self.bounds.size.height));

    currMovingDot.position = mousePoint;
    currMovingDot = nil;
    [self setNeedsDisplay:YES];
}

- (Dot *)clickedDotForPoint:(NSPoint)point {
    // DOT_NUCLEUS_RADIUS is the size of the 
    // dot's internal "handle"
    for( Dot *dot in dots ){
        if( (abs(dot.position.x - point.x) <= DOT_NUCLEUS_RADIUS) &&
            (abs(dot.position.y - point.y) <= DOT_NUCLEUS_RADIUS)) {
            return dot;
        }
    }
    return nil;
}

Dot.h

#define DOT_NUCLEUS_RADIUS (5)

@interface Dot : NSObject {
    NSPoint position;
}

@property (assign) NSPoint position;

- (void)draw;

@end

Dot.m

#import "Dot.h"

@implementation Dot

@synthesize position;

- (void)draw {
    //!!!: Demo only: assume that focus is locked on a view.
    NSColor *clr = [NSColor colorWithDeviceRed:0.3 
                                         green:0.2 
                                          blue:0.8 
                                         alpha:1];
    // Draw a nice border
    NSBezierPath *outerCirc;
    outerCirc = [NSBezierPath bezierPathWithOvalInRect:
                         NSMakeRect(position.x - 23, position.y - 23, 46, 46)];
    [clr set];
    [outerCirc stroke];
    [[clr colorWithAlphaComponent:0.7] set];
    [outerCirc fill];
    [clr set];
    // Draw the "handle"
    NSRect nucleusRect = NSMakeRect(position.x - DOT_NUCLEUS_RADIUS,
                                    position.y - DOT_NUCLEUS_RADIUS,
                                    DOT_NUCLEUS_RADIUS * 2,
                                    DOT_NUCLEUS_RADIUS * 2);
    [[NSBezierPath bezierPathWithOvalInRect:nucleusRect] fill];
}

@end

如您所见,Dot类非常轻量级,并使用bezier路径绘制。 superview可以处理用户交互。

相关问题