Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:56:38+00:00 2026-06-17T15:56:38+00:00

I am trying to draw an arrow using my finger on the screen. the

  • 0

I am trying to draw an arrow using my finger on the screen. the idea was that by touching the screen I set the initial coordinates of my arrow, and as I dragged on the screen the arrow would extend and follow my finger. the height and width of the arrow will be the same, its the size of the arrow that matters. The arrow will be longer as I drag it away from the starting point. I tried dong it with something like this:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIGraphicsBeginImageContext(CGSizeMake(1536, 2048));

    UITouch *touch = [touches anyObject];
    CGPoint p1 = [touch locationInView:self.view];

    CGSize size;
    size.width = 50;
    size.height = 400;

    CGContextRef context = UIGraphicsGetCurrentContext();

    [self drawArrowWithContext:context atPoint:p1 withSize:size lineWidth:4 arrowHeight:20 andColor:[UIColor whiteColor]];

    // converts your context into a UIImage
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Adds that image into an imageView and sticks it on the screen.
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
}

and

- (void) drawArrowWithContext:(CGContextRef)context atPoint:(CGPoint)startPoint withSize: (CGSize)size lineWidth:(float)width arrowHeight:(float)aheight andColor:(UIColor *)color
{
    float width_wing = (size.width - width) / 2;
    float main = size.height-aheight;

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextSetStrokeColorWithColor(context, [color CGColor]);

    CGPoint rectangle_points[] = {
        CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
        CGPointMake(startPoint.x + width_wing, startPoint.y + main),
        CGPointMake(startPoint.x + 0.0, startPoint.y + main), // left point
        CGPointMake(startPoint.x + size.width / 2, startPoint.y + size.height),

        CGPointMake(startPoint.x + size.width, startPoint.y + main), // right point

        CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + main),

        CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + 0.0),
        CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
    };

    CGContextAddLines(context, rectangle_points, 8);

    CGContextFillPath(context);
}

The arrow does appear on the screen if I run the code from the touches moved in a normal IBOutlet, but that was not the idea. I haven’t managed to get this code to work yet, but I think that even if it worked, it would cause a crash, since I am deleting and redrawing the shape each time. Is this the right approach? What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:56:39+00:00Added an answer on June 17, 2026 at 3:56 pm

    Basically, there are some different options.

    • Stick to the UIImageView approach and stop recreating the image view all the time. Keep a reference to that UIImageView in the class detecting the touch events and just replace the image if something changed. Might be worth the extra effort for off-screen-drawing if you need the image for something else.
    • Implement the arrow drawing dynamically in an extra view.

    The second one I will describe here briefly:

    The class detecting the event needs a member variable/property, ArrowView *arrowView and something to remember the start point, CGPoint startPoint maybe.
    ArrowView needs properties for the arrow parameters, CGPoint arrowStart, CGSize arrowSize.
    In the touch event handler, do

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [touches anyObject];
        CGPoint position = [touch locationInView:self.view];
    
        [self.startPoint startFrom:position];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [touches anyObject];
        CGPoint next = [touch locationInView:self.view];
    
        CGSize size;
        size.width = next.x - self.startPoint.x;
        size.height = next.y - self.startPoint.y;
    
        self.arrowView.arrowPoint = self.startPoint;
        self.arrowView.arrowSize = size;
    
        [self.arrowView setNeedsDisplay];
    }
    

    In ArrowView:

    - (void)drawRect:(CGRect)rect {
        [super drawRect:rect];
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        // Now do the drawing stuff here using that context!
        // self.arrowSize / self.arrowPoint do contain your drawing parameters.
        ...
    }
    

    I hope that gives you an idea.

    For further reading, start here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to draw a circle on an image using cv.Circle, I realized that
I am trying to draw in a web worker using html5 canvas. The worker
I'm trying to draw an open rectangle using a Polygon: int[] xPoints = {1,1,3,3};
I'm just trying to draw a line with JavaScript. I would like it to
I am trying to draw a circle using mouse on the canvas using mouse
I'm trying to draw lots of circles on a sphere using shaders. The basic
how to draw an arrow in android touch event? i m trying to draw
I'm trying to draw a graph using the coreplot library. I'm looking for a
I'm trying to draw selection/focus frame in my custom windows forms control using visual
I am trying to draw a line using the Graphics 2D but then the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.