Create Line with arrow in iOS Objective C

Solution:

In iOS you can create a Line using CGContextref.The below code is used for draw a line in a view in x1,y1, is 0,0 and x2,y2 is 20,20.

Also add the cap in the end of the line to show the line as arrow



    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(00,20,20)
    CGContextClearRect(ctx, rect);


    CGContextSetStrokeColorWithColor(ctx, color.CGColor);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 10);
    CGContextSetAlpha(ctx, 1);
    CGContextMoveToPoint(ctx, 0, 0);

//add the cap    
        CGPoint fPoint=CGPointMake(0,0);
        CGPoint ePoint=CGPointMake(20,20);
        CGFloat capHeight = 1 * 4.0f;
        CGFloat angle = [self angleWithFirstPoint:fPoint secondPoint:ePoint];
        CGPoint p1 = [self pointWithAngle:angle + 7.0f * M_PI / 8.0f distance:capHeight];
        CGPoint p2 = [self pointWithAngle:angle - 7.0f * M_PI / 8.0f distance:capHeight];
        CGPoint endPointOffset = [self pointWithAngle:angle distance:1];
        NSLog(@"Down");
        p1 = CGPointMake(ePoint.x + p1.x, ePoint.y + p1.y);
        p2 = CGPointMake(ePoint.x + p2.x, ePoint.y + p2.y);
       
        CGContextMoveToPoint(ctx, p1.x, p1.y);
    CGContextAddLineToPoint(ctx, 20, 20);
    CGContextStrokePath(ctx);
    CGContextDrawPath(ctx, kCGPathFillStroke);

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift