objective-c – 图像不想拖动

objective-c – 图像不想拖动,第1张

概述我正在关注Aaron针对Mac OS X的 Cocoa编程的第23章. 我必须从视图中拖出一个字母并将其复制到另一个应用程序中,如文本编辑. 这是我发现问题的代码部分: - (void) mouseDragged:(NSEvent *)theEvent{ NSPasteboard* pb=[NSPasteboard pasteboardWithName: NSDragPboard]; 我正在关注Aaron针对Mac OS X的 Cocoa编程的第23章.
我必须从视图中拖出一个字母并将其复制到另一个应用程序中,如文本编辑.
这是我发现问题的代码部分:

- (voID) mouseDragged:(NSEvent *)theEvent{    NSPasteboard* pb=[NSPasteboard pasteboarDWithname: NSDragPboard];    NSPoint down=[mouseDownEvent locationInWindow];    NSPoint drag=[theEvent locationInWindow],p;    NSSize size=[string sizeWithAttributes: attributes];    NSRect imageBounds;    NSImage* image=[NSImage alloc];    float distance;    distance= hypot(down.x-drag.x,down.y-drag.y);    if(distance<3 || [string length]==0)    return;    image=[image initWithSize: size];    imageBounds.origin=NSZeroPoint;    imageBounds.size=size;    [image lockFocus];    [self drawStringCenteredIn: imageBounds];    [image unlockFocus];    p=[self convertPoint: down fromVIEw: nil];    p.x= p.x - size.wIDth/2;    p.y= p.y - size.height/2;    [self writetoPasteboard: pb];    [self dragImage: image at: p offset: NSZeroSize event: mouseDownEvent pasteboard: pb source: self slIDeBack: YES];}

哪里:
– mouseDownEvent是先前保存的事件,当用户点击了lecter(mouseDown事件)时;
– string是一个NSMutableAttributesstring,一个最大值为1的字符串,包含要显示在视图中的lecter;

当事件发生时,视图上已经显示了一个字母(所以字符串的长度为1).如果我忘记了一些重要的信息,请询问.

问题:拖放 *** 作工作正常,但问题是当我拖动图像时,我没有看到图像从它的原始位置移位.
这是我在拖信时看到的:

 

这是我应该看到的:

所以这封信应该取代,但这不会发生.我不认识这个问题的原因,我认为drawImageCenteredIn方法应该可以正常工作.这就是这个方法的代码:

- (voID) drawStringCenteredIn: (NSRect) rect{    NSSize size=[string sizeWithAttributes: attributes];    NSPoint origin;    origin.x= rect.origin.x+ (rect.size.wIDth+size.wIDth)/2;    origin.y=rect.origin.y + (rect.size.height+size.height)/2;    [string drawAtPoint: origin withAttributes: attributes];}
解决方法 这很简单.这两行中的符号不​​正确:

origin.x= rect.origin.x+ (rect.size.wIDth-size.wIDth)/2;  origin.y= rect.origin.y + (rect.size.height-size.height)/2;

这是更正的方法:

- (voID) drawStringCenteredIn: (NSRect) rect{    NSSize size=[string sizeWithAttributes: attributes];    NSPoint origin;    origin.x= rect.origin.x+ (rect.size.wIDth-size.wIDth)/2;    origin.y= rect.origin.y + (rect.size.height-size.height)/2;    [string drawAtPoint: origin withAttributes: attributes];}
总结

以上是内存溢出为你收集整理的objective-c – 图像不想拖动全部内容,希望文章能够帮你解决objective-c – 图像不想拖动所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1071733.html

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

发表评论

登录后才能评论

评论列表(0条)

保存