我正在尝试拖动一个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绘制了rect 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");
}有什么想法吗?
发布于 2013-08-04 21:03:09
您总是将myRect设置为drawRect:内的常量值。您想要的似乎是在myRect中完全不设置drawRect:,并使用其当前值绘制它。所以就像这样:
- (void)drawRect:(NSRect)rect
{
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(myContext, 1, 0, 0, 1);
CGContextFillRect(myContext, myRect);
NSLog(@"drawRect: called");
}https://stackoverflow.com/questions/18044646
复制相似问题