【问题标题】:mouseDragged: on CGRect鼠标拖动:在 CGRect 上
【发布时间】:2013-08-04 15:41:23
【问题描述】:

我正在尝试拖动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

这是我的drawRect:

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

}

有什么想法吗?

【问题讨论】:

  • 你能展示你的drawRect:方法吗?
  • 我添加了 drawRect: 方法

标签: cocoa core-graphics quartz-core


【解决方案1】:

您总是将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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多