1.绘制箭头的三种方式
// Drawing code // CGContextRef con = UIGraphicsGetCurrentContext(); // //draw a black(by default) vertical line,the shaft of the arrow 箭杆 // CGContextMoveToPoint(con, 100, 100); // CGContextAddLineToPoint(con, 100, 19); // CGContextSetLineWidth(con, 20); // CGContextStrokePath(con); // //draw a red triangle,the point of the arrow // CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]); // CGContextMoveToPoint(con, 80, 25); // CGContextAddLineToPoint(con, 100, 0); // CGContextAddLineToPoint(con, 120, 25); // CGContextFillPath(con); // //snip a triangle out of the shaft by drawing in clear blend mode // CGContextMoveToPoint(con, 90, 100); // CGContextMoveToPoint(con, 100, 90); // CGContextMoveToPoint(con, 110, 100); // CGContextSetBlendMode(con, kCGBlendModeClear); // CGContextSetAlpha(con, 1.0); // CGContextFillPath(con); //当需要获取CGPath时,可以使用UIBezierPath // UIBezierPath *p = [UIBezierPath bezierPath]; // // [p moveToPoint:CGPointMake(100, 100)]; // [p addLineToPoint:CGPointMake(100, 19)]; // [p setLineWidth:20]; // [p stroke]; // [[UIColor redColor] set]; // [p removeAllPoints]; // [p moveToPoint:CGPointMake(80, 25)]; // [p addLineToPoint:CGPointMake(100, 0)]; // [p addLineToPoint:CGPointMake(120, 25)]; // [p fill]; // // [p removeAllPoints]; // [p moveToPoint:CGPointMake(90, 101)]; // [p addLineToPoint:CGPointMake(100, 90)]; // [p addLineToPoint:CGPointMake(110, 101)]; // [p fillWithBlendMode:kCGBlendModeClear alpha:1.0]; //Clipping CGContextRef con = UIGraphicsGetCurrentContext(); //punch triangular hole in context clipping region CGContextMoveToPoint(con, 90, 100); CGContextAddLineToPoint(con, 100, 90); CGContextAddLineToPoint(con, 110, 100); CGContextClosePath(con); CGContextAddRect(con, CGContextGetClipBoundingBox(con)); CGContextEOClip(con); //draw the vertical line CGContextMoveToPoint(con, 100, 100); CGContextAddLineToPoint(con, 100, 19); CGContextSetLineWidth(con, 20); CGContextStrokePath(con); //draw the red triangle.the point of the arrow CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]); CGContextMoveToPoint(con, 80, 25); CGContextAddLineToPoint(con, 100, 0); CGContextAddLineToPoint(con, 120, 25); CGContextFillPath(con);
效果:
CGContextRef con = UIGraphicsGetCurrentContext(); [[self arrowImage] drawAtPoint:CGPointMake(0, 0)]; for (int i=0; i<3; i++) { CGContextTranslateCTM(con, 20, 100); CGContextRotateCTM(con, 30*M_PI/180); CGContextTranslateCTM(con, -20, -100); [[self arrowImage] drawAtPoint:CGPointMake(0, 0)]; } // } -(UIImage *)arrowImage { UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0); CGContextRef con = UIGraphicsGetCurrentContext(); //punch triangular hole in context clipping region CGContextMoveToPoint(con, 10, 100); CGContextAddLineToPoint(con, 20, 90); CGContextAddLineToPoint(con, 30, 100); CGContextClosePath(con); CGContextAddRect(con, CGContextGetClipBoundingBox(con)); CGContextEOClip(con); //draw the vertical line CGContextMoveToPoint(con, 20, 100); CGContextAddLineToPoint(con, 20, 19); CGContextSetLineWidth(con, 20); CGContextStrokePath(con); //draw the 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); UIImage *im = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return im; }
当我不是绘制在(0,0)时就有问题,好像是由坐标系统改变导致的
CGContextRef con = UIGraphicsGetCurrentContext(); [[self arrowImage] drawAtPoint:CGPointMake(150, 200)]; for (int i=0; i<12; i++) { CGContextTranslateCTM(con, 20, 100); CGContextRotateCTM(con, 30*M_PI/180); CGContextTranslateCTM(con, -20, -100); [[self arrowImage] drawAtPoint:CGPointMake(150, 200)]; }
尚未找到原因,有知道的童鞋,欢迎告知,一起交流
时间: 2024-10-09 22:56:13