How do i get rid of a line drawn by drawRect to disappear .05 seconds after the user touches the screen? - core-graphics

I have the touchesBegan void before this which triggers this event,
but i need this line to show, wait for so many seconds, then disappear:
- (void)drawRect:(CGRect)rect {
// Drawing code
//Make a simple rectangle and fill background BLUE with half Opeque
CGContextRef context = UIGraphicsGetCurrentContext();
//[[UIColor blueColor] set];
//CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
//rect = CGRectMake(20, 20, 40, 40);
//CGContextFillRect(context, rect);
//Make a simple RED line from 0,0 to 100, 100 in a FRAME
//CGContextSetRGBFillColor(context, 100.0, 0.0, 0.0, 1.0);
[[UIColor redColor] set];
CGContextMoveToPoint(context, _spaceShip.center.x, _spaceShip.center.y);
CGContextAddLineToPoint( context, nextLocX, nextLocY);
CODE FOR MAKING THE LINE DISAPPEAR
}

Redraw the frame without that line would be the obvious solution.
Draw over it in the background color would be a less-pretty solution that might in some cases run faster.

Related

How to override draw method on PDFAnnotation IOS-PDFKIT

I followed another StackOverflow post that explains how i could override the draw method of a PDFAnnotation so i could draw a picture instead of a traditional PDFAnnotation.
But sadly i was not able to achieve that and the annotation that is drawn on top of my pdf is still a regular one.
This is the code that i used :
#implementation PDFImageAnnotation { UIImage * _picture;
CGRect _bounds;};
-(instancetype)initWithPicture:(nonnull UIImage *)picture bounds:(CGRect) bounds{
self = [super initWithBounds:bounds
forType:PDFAnnotationSubtypeWidget
withProperties:nil];
if(self){
_picture = picture;
_bounds = bounds;
}
return self;
}
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
[_picture drawInRect:_bounds];
CGContextRestoreGState(context);
UIGraphicsPushContext(context);
};
#end
Does someone know how i could override the draw method so i could draw a custom Annotation ?
Thank You !
ps: i also tried to followed the tutorial on the apple dev site.
UPDATE :
Now i'm able to draw pictures using CGContextDrawImage but i'm not able to flip coordinates back in place. when i do that mi pictures are not drawn and it seems that they are put outside of the page but i'm not sure.
This is my new code :
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, _pdfView.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, _bounds, _picture.CGImage);
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
I also tried to follow the tutorial on the Apple dev site.
Which one?
Custom Graphics
Adding Custom Graphics to a PDF
Because both include UIGraphicsPushContext(context) & CGContextSaveGState(context) calls, but your code doesn't. Do not blindly copy & paste examples, try to understand them. Read what these two calls do.
Fixed code:
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
[_picture drawInRect:_bounds];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
The image was drawn with CGRectMake(20, 20, 100, 100). It's upside down, because PDFPage coordinates are flipped (0, 0 = bottom/left). Leaving it as an exercise for OP.
Rotation
Your rotation code is wrong:
CGContextTranslateCTM(context, 0.0, _pdfView.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, _bounds, _picture.CGImage);
It's based on _pdfView bounds, but it should be based on the image bounds (_bounds). Here's the correct one:
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, _bounds.origin.x, _bounds.origin.y + _bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[_picture drawInRect:CGRectMake(0, 0, _bounds.size.width, _bounds.size.height)];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}

Lines drawn with core graphics that are set to the same width sometimes vary in size when drawn

Here is a picture of two lines drawn in a UITableViewCell with the same function, and same width, and color
As you can see the bottom line is a lot thicker than the other line.
The code I am using for drawing:
[CSDrawing drawLineWithColor:[UIColor blackColor] width:1.0 yPosition:1.0 rect:rect];
[CSDrawing drawLineWithColor:[UIColor blackColor] width:1.0 yPosition:CGRectGetMaxY(rect) - 3.0 rect:rect]; // draw a line on top and bottom
+(void)drawLineWithColor:(UIColor *)color width:(CGFloat)width yPosition:(CGFloat)yPosition rect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextMoveToPoint(context, 0.0, yPosition);
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), yPosition);
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetLineWidth(context, width);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
The problem was with the backgroundView being stretched to fit the content of the cell when the cell was being reused. When the cell was bigger the pixels were stretched. This is solved be setting the contentMode property to UIViewContentModeRedraw

need assistance regarding growing & shrinking circle from centre in quartz-2d

I am currently working on drawing app, in which have a slider to increase and decrease line width. I just want to do a simple thing that a circle in front of slider to present a width. I easily did that but its not growing and shrinking from centre, its growing and shrinking from top x, y, here is the code
- (UIImage *)circleOnImage:(int)size
{
UIGraphicsBeginImageContext(CGSizeMake(25, 25));
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setFill];
CGContextTranslateCTM(ctx, 12, 12);//also try to change the coordinate but didn't work
CGRect circleRect = CGRectMake(0, 0, size, size);
CGContextFillEllipseInRect(ctx, circleRect);
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return retImage;
}
Try
CGContextTranslateCTM(ctx, 12.5, 12.5);
CGRect circleRect = CGRectMake(-size/2., -size/2., size, size);

drawing dotted lines from UIViewController

I have a UIViewController which has a UIView in it and I want to draw a straight dotted line on it. What is the easiest and performant way to do this? How do I do this? It will just be a straight horizontal dotted line of width 150 with gray color. I've looked around and it seems that using
CGContextSetLineDash is the solution and here's what I have so far:
-(void) drawRect:(CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 100);
CGFloat dashes[] = {1,1};
CGContextSetLineDash(context, 2.0, dashes, 2);
CGContextStrokePath(context);
}
CGContextStrokePath draws the current path, however, you never add any path to your context, so nothing is drawn.
Add the following two lines before your CGContextStrokePath call and you should see something:
CGContextMoveToPoint(context, 0, self.bounds.size.height * 0.5);
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height * 0.5);
If you haven't already set your stroke color, you should also add something like this (50% gray):
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0);
I'm not sure if the result is really what you want, a 100 point dotted line with a pattern of {1, 1} will look more like an rectangle with a pinstripe pattern than like a dotted line. If you want your dots to be circles, you will have to draw those manually in some sort of loop.
Try this:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetLineWidth(context, 2.0);
CGFloat lengths[] = {0, 8};
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineDash(context, 0.0f, lengths, 2);
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 200, 10);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);

Rollback drawRect:

Is there any way to quit from drawRect: without refresh the current UIView?
For example:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
//I would like to return from here and rollback all changes in drawRect:
}
Thanks!
If you haven't started drawing into the context, it might be possible to just return from drawRect:. You'd have to set the clearsContextBeforeDrawing property of the view to NO before.
After starting to draw in the context, the view/layer will always update its content.