gpt4 book ai didi

objective-c - Cocoa 中的绘图选择框(橡皮筋、行进的 Ant ),Objective C

转载 作者:太空狗 更新时间:2023-10-30 03:22:37 27 4
gpt4 key购买 nike

我目前已经使用鼠标事件实现了一个简单的选择框,并在鼠标拖动时重新绘制了一个矩形。这是我的代码:

-(void)drawRect:(NSRect)dirtyRect
{
if (!NSEqualRects(self.draggingBox, NSZeroRect))
{
[[NSColor grayColor] setStroke];
[[NSBezierPath bezierPathWithRect:self.draggingBox] stroke];
}
}

#pragma mark Mouse Events

- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
self.draggingBox = NSMakeRect(pointInView.x, pointInView.y, 0, 0);
[self setNeedsDisplay:YES];
}

- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
_draggingBox.size.width = pointInView.x - (self.draggingBox.origin.x);
_draggingBox.size.height = pointInView.y - (self.draggingBox.origin.y);
[self setNeedsDisplay:YES];
}

- (void)mouseUp:(NSEvent *)theEvent
{
self.draggingBox = NSZeroRect;
[self setNeedsDisplay:YES];
}

引用:http://cocoadev.com/HowToCreateWalkingAnts

问题:

这是最有效的方法吗?如果 View 很复杂,在主视图上绘制透明 View 而不是在鼠标拖动期间不断重绘 View 是否更有效 (http://www.cocoabuilder.com/archive/cocoa/99877-drawing-selection-rectangle.html)?这是怎么做到的?我似乎找不到任何示例。

最佳答案

如果需要,您可以使用 QuartzCore 为您制作“行进的 Ant ”动画。这也让您完全摆脱手动绘制橡皮筋选择框的世界。您只需定义定义该框的路径,然后让 Core Animation 负责绘制框并为其设置动画。

在 XIB 的“View Effects”Inspector 中,打开“Core Animation”,然后你可以这样做:

#import <QuartzCore/QuartzCore.h>
#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

这样,Core Animation 会为您处理行进中的 Ant 。

marching ants demo

关于objective-c - Cocoa 中的绘图选择框(橡皮筋、行进的 Ant ),Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com