gpt4 book ai didi

cocoa - 鼠标拖动 : on CGRect

转载 作者:行者123 更新时间:2023-12-03 17:40:12 28 4
gpt4 key购买 nike

我试图拖动CGRect,但没有任何反应。矩形保留在同一个地方。这是我的 mouseDragged: 代码:

-(void)mouseDragged:(NSEvent *)theEvent{

NSPoint eventLocation = [theEvent locationInWindow];
NSPoint location = [self convertPoint:eventLocation fromView:self];

int locationX = location.x;
int locationY = location.y;

[self setNeedsDisplay:TRUE];

if(CGRectContainsPoint(myRect, location)){

myRect.origin.x = locationX;
myRect.origin.y = locationY;
}
}

此代码位于 NSView 内部。我在 drawRect 中绘制矩形 myRect:

这是我的绘制矩形:

- (void)drawRect:(NSRect)rect
{

int width = self.bounds.size.width;
int height = self.bounds.size.height;

myRect = CGRectMake((width/2)-50, (height /2)-50, 100, 100);


CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(myContext, 1, 0, 0, 1);
CGContextFillRect(myContext, myRect);

NSLog(@"drawRect: called");

}

有什么想法吗?

最佳答案

您始终将 myRect 设置为 drawRect: 内的常量值。看来您想要的是在 drawRect: 中根本不设置 myRect 并用其当前值绘制它。所以像这样:

- (void)drawRect:(NSRect)rect
{
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(myContext, 1, 0, 0, 1);
CGContextFillRect(myContext, myRect);

NSLog(@"drawRect: called");
}

关于cocoa - 鼠标拖动 : on CGRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18044646/

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