objective-c – Cocoa中的绘图选择框(橡皮筋,行进蚂蚁),ObjectiveC

objective-c – Cocoa中的绘图选择框(橡皮筋,行进蚂蚁),ObjectiveC,第1张

概述我目前使用鼠标事件实现了一个简单的选择框,并在鼠标拖动时重绘了一个矩形.这是我的代码: -(void)drawRect:(NSRect)dirtyRect{if (!NSEqualRects(self.draggingBox, NSZeroRect)){ [[NSColor grayColor] setStroke]; [[NSBezierPath bezierPathWit 我目前使用鼠标事件实现了一个简单的选择框,并在鼠标拖动时重绘了一个矩形.这是我的代码:

-(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);    [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

问题:

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

解决方法 如果需要,您可以使用QuartzCore为您设置“行进蚂蚁”的动画.这使您完全脱离了手动绘制橡皮带选择框的世界.您只需定义定义该框的路径,让Core Animation负责绘制框,并为其设置动画.

在XIB的“视图效果”检查器中,打开“核心动画”,然后您可以执行以下 *** 作:

#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,point.y);    CGPathAddlinetoPoint(path,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将为您提供行军蚂蚁.

总结

以上是内存溢出为你收集整理的objective-c – Cocoa中的绘图选择框(橡皮筋,行进蚂蚁),ObjectiveC全部内容,希望文章能够帮你解决objective-c – Cocoa中的绘图选择框(橡皮筋,行进蚂蚁),ObjectiveC所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1035360.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存