I am currently following the codes in Programming iOS 7 by Matt Neuburg.
I have typed the following codes into my custom view (ArrowsView).
#import "ArrowsView.h"
@implementation ArrowsView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.opaque = NO;
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[[self arrowImage] drawAtPoint:CGPointMake(0, 0)];
// UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0);
// // obtain the current graphics context.
//// CGContextRef con = UIGraphicsGetCurrentContext();
// CGContextTranslateCTM(con, 80, 0);
// // draw the arrow into the current context.
// // draw the black (by default) vertical line, the shaft of the arrow.
// CGContextMoveToPoint(con, 20, 100);
// CGContextAddLineToPoint(con, 20, 19);
// CGContextSetLineWidth(con, 20);
// CGContextStrokePath(con);
// // draw a red triangle, the point of the arrow.
// CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
// CGContextMoveToPoint(con, 0, 25);
// CGContextAddLineToPoint(con, 20, 0);
// CGContextAddLineToPoint(con, 40, 25);
// CGContextFillPath(con);
// // snip a triangle out of the shaft by drawing in clear blend mode.
// CGContextMoveToPoint(con, 10, 101);
// CGContextAddLineToPoint(con, 20, 90);
// CGContextAddLineToPoint(con, 30, 101);
// CGContextSetBlendMode(con, kCGBlendModeClear);
// CGContextFillPath(con);
// UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// [im drawAtPoint:CGPointMake(0, 0)];
}
- (UIImage*) arrowImage {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0);
// obtain the current graphics context.
CGContextRef con = UIGraphicsGetCurrentContext();
// draw it at (0, 0);
CGContextTranslateCTM(con, 80, 0);
// draw the arrow into the current context.
// draw the black (by default) vertical line, the shaft of the arrow.
CGContextMoveToPoint(con, 20, 100);
CGContextAddLineToPoint(con, 20, 19);
CGContextSetLineWidth(con, 20);
CGContextStrokePath(con);
// draw a red triangle, the point of the arrow.
CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
CGContextMoveToPoint(con, 0, 25);
CGContextAddLineToPoint(con, 20, 0);
CGContextAddLineToPoint(con, 40, 25);
CGContextFillPath(con);
// snip a triangle out of the shaft by drawing in clear blend mode.
CGContextMoveToPoint(con, 10, 101);
CGContextAddLineToPoint(con, 20, 90);
CGContextAddLineToPoint(con, 30, 101);
CGContextSetBlendMode(con, kCGBlendModeClear);
CGContextFillPath(con);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return im;
}
@end
Note the large block of commented out drawing calls in drawRect:. I am able to get that block of code to draw what I need to see on screen.
However, I am unable to get the preceding line of code to work in drawRect:. Specifically, this line of code did not work:
[[self arrowImage] drawAtPoint:CGPointMake(0, 0)];
It seems that I am unable to send drawAtPoint: to [self arrowImage]. I've checked, and the return value is not nil.
Can someone help me with this?
Thank you.
Remove
CGContextTranslateCTM(con, 80, 0);
from thearrowImage
method and it will work.