Remove a drawn NSRect or other drawn object - objective-c

How do I remove a drawn NSRect or other object other than drawing another rect over it?

Assuming you drew the rect in drawRect:, all you need to do would be to exclude the rect from the list of objects to be drawn and call [view setNeedsDisplayInRect:theRect]. theRect here would be the CGRect equivalent.

Related

cocoa - draw image in NSView

I'm having difficulties understanding why the below code doesn't work, what I want to achieve is an image being displayed in the top left corner of a NSView, but nothing is showing...
NSImage *map0 = [NSImage imageNamed:#"map0.png"];
NSRect rect = NSMakeRect(0, 0, 400, 400);
[map0 drawInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:1.0f];
[map drawRect:rect];
EDIT:
map is the NSView into which I would like to draw the image into
You never call drawRect: directly. This routine has various pre-conditions that are provided by Cocoa, such as the creation of a CGContextRef. You implement drawRect:. Cocoa calls it.
Your drawInRect:fromRect:operation:fraction: call should be put into the drawRect: of map, which should be a subclass of NSView. This specific problem is usually better solved with an NSImageView rather than a custom NSView, but if the drawing is more complex, then a custom NSView is appropriate.

NSRect rounded corners

Is there a simple way to add rounded corners to NSRect elements in Objective-C? Currently we're applying a PNG image that simulates corners to this:
NSRect newFrame = NSMakeRect(0, 0, size.width, size.height);
But, performance becomes an issue because there are many instances of this NSRect along with the image being rendered with Core Animation. Perhaps rendering a native NSRect with rounded edges would be better from a performance standpoint? Do said edges look smooth (anti-aliased) when rendered with Core Animation?
NSRect is a struct containing an NSPoint and an NSSize, so I think you mean anything that accepts NSRects (so subclasses of NSView). All NSView subclass layers respond appropriately to -cornerRadius (except something about NSScrollView).
self.view.layer.masksToBounds = YES;
self.view.layer.cornerRadius = 10.0;

Move an NSRect with mouse

how can I create an NSRect where it is selectable and can be moved around inside a view? Would it be a CGRect or an NSRect to accomplish this? what would I add to:
NSRect viewFrame = NSMakeRect(0, 0, 100, 100);
Would I first have to make it selectable and then use an NSEvent, or could I have the NSRect origin be equal to my mouse position in an NSTracking?
NSRect and CGRect are just structures that contain the data that defines a rectangle. If you want to draw a rectangle on the screen, you'll need to create a view that draws the rectangle. To move that rectangle, you'll tell the view to draw the rectangle in different locations, or just move the view around.

How do I MOVE a circle drawn in a subclass of UIView by overwriting the method "drawRect"?

I'm trying to figure out what i'm doing wrong but i just don't get it. Here is what i want to do:
I want to draw a circle somewhere on the screen of the iPhone and then i want the circle always to be displayed at the position where the user currently taps on the screen.
I started by creating a subclass of UIView and adding the following lines into the "drawRect" method:
- (void)drawRect:(CGRect)rect {
//Create the main view!
CGContextRef mainscreen = UIGraphicsGetCurrentContext();
//Draw the dot
//will be a circle cause rectangle is a square
CGRect dotRect = CGRectMake(50, 80, 100, 100);
[[UIColor blueColor] set];
CGContextStrokeEllipseInRect(mainscreen, dotRect);
CGContextFillEllipseInRect(mainscreen, dotRect);
}
The appears just fine but now I have no idea how to make it move around on the screen I've tried several things and nothing worked.
To draw the dot in a different location, change the origin of dotRect. To figure out where to draw it, implement -touchesBegan:withEvent: and -touchesMoved:withEvent: and record the location where the touches are occuring.

Converting a CGRect of a object in a subview in UITableViewCell in UITable to CGRect with respect to self.view

I want to convert a CGRect "aRect" of a object in a subview in UITableViewCell in UITableView to CGRect with respect to self.view.
The problem is that aRect is with respect to the subview in the table cell.
But i want to make it so that it is with respect to self.view.
What i tried is this
rect = [self.view convertRect:aRect.frame toView:cellView];
but it doesn't work.
So how to convert it?
Since convertRect:toView: actually deals with a CGRect in the receiver’s coordinate system, maybe [cellView convertRect:theFrame toView:self.view] (i.e. swapping the two views in your call) will work as you intended.