Drawing selection box (rubberbanding, marching ants) in Cocoa, ObjectiveC

后端 未结 4 1131
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 07:28

I\'ve currently implemented a simple selection box using mouse events and redrawing a rectangle on mouse drag. Here\'s my code:

-(void)drawRect:(NSRect)dirty         


        
4条回答
  •  庸人自扰
    2020-12-13 08:13

    You can use QuartzCore to animate the "marching ants" for you, if you want. This gets you completely out of the world of manually drawing the rubber-banded selection box, too. You just define the path that defines that box, and let Core Animation take care of drawing the box, as well as animating it.

    In the XIB's "View Effects" Inspector, turn on "Core Animation", and then you can do something like:

    #import 
    #import "CustomView.h"
    
    @interface CustomView ()
    
    @property (nonatomic) NSPoint startPoint;
    @property (nonatomic, strong) CAShapeLayer *shapeLayer;
    
    @end
    
    @implementation CustomView
    
    #pragma mark Mouse Events
    
    - (void)mouseDown:(NSEvent *)theEvent
    {
        self.startPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    
        // create and configure shape layer
    
        self.shapeLayer = [CAShapeLayer layer];
        self.shapeLayer.lineWidth = 1.0;
        self.shapeLayer.strokeColor = [[NSColor blackColor] CGColor];
        self.shapeLayer.fillColor = [[NSColor clearColor] CGColor];
        self.shapeLayer.lineDashPattern = @[@10, @5];
        [self.layer addSublayer:self.shapeLayer];
    
        // create animation for the layer
    
        CABasicAnimation *dashAnimation;
        dashAnimation = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"];
        [dashAnimation setFromValue:@0.0f];
        [dashAnimation setToValue:@15.0f];
        [dashAnimation setDuration:0.75f];
        [dashAnimation setRepeatCount:HUGE_VALF];
        [self.shapeLayer addAnimation:dashAnimation forKey:@"linePhase"];
    }
    
    - (void)mouseDragged:(NSEvent *)theEvent
    {
        NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    
        // create path for the shape layer
    
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, self.startPoint.x, self.startPoint.y);
        CGPathAddLineToPoint(path, NULL, self.startPoint.x, point.y);
        CGPathAddLineToPoint(path, NULL, point.x, point.y);
        CGPathAddLineToPoint(path, NULL, point.x, self.startPoint.y);
        CGPathCloseSubpath(path);
    
        // set the shape layer's path
    
        self.shapeLayer.path = path;
    
        CGPathRelease(path);
    }
    
    - (void)mouseUp:(NSEvent *)theEvent
    {
        [self.shapeLayer removeFromSuperlayer];
        self.shapeLayer = nil;
    }
    
    @end
    

    This way, Core Animation takes care of the marching ants for you.

    marching ants demo

提交回复
热议问题