移动NSView直到它到达边框

Seb*_*ler 4 cocoa objective-c 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])];
}
Run Code Online (Sandbox Code Playgroud)

奇迹般有效.我现在遇到的问题:如何防止矩形移动到画布外?

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

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

问候

苹果电脑

Jos*_*ell 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];
}
Run Code Online (Sandbox Code Playgroud)

你基本上会检查相同的边界mouseUp:.

更新:您还应该查看"视图编程指南",它将引导您创建可拖动的视图:创建自定义视图.


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

在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;
}
Run Code Online (Sandbox Code Playgroud)

Dot.h

#define DOT_NUCLEUS_RADIUS (5)

@interface Dot : NSObject {
    NSPoint position;
}

@property (assign) NSPoint position;

- (void)draw;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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