/ / माउसड्रैग्ड: CGRect पर - कोको, कोर-ग्राफिक्स, क्वार्ट्ज-कोर

माउस ड्रैग: सीजीआरईटी - कोको, कोर-ग्राफिक्स, क्वार्ट्ज-कोर पर

मैं खींचने की कोशिश कर रहा हूँ 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;
}
}

यह कोड a . के अंदर है NSView. मैं रेक्ट ड्रा करता हूँ 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");

}

कोई विचार?

उत्तर:

जवाब के लिए 2 № 1

आप हमेशा सेटिंग कर रहे हैं 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");
}