Draw line on Swiping Finger with Arrow following the Swipe path
Asked Answered
G

1

8

I am creating an app, in which when I am swiping my finger on screen, that time I am drawing line using code.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),3.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
}

And I am also moving arrow at the same time at that line using code....

-(void)moveBallConstantly
{
 [UIView animateWithDuration:0.01f animations: ^{
         [appDel.ballImageView setCenter:CGPointMake(appDel.ballImageView.center.x +        (x/increamentFraction), appDel.ballImageView.center.y + (y/increamentFraction))];
   }];
}

Its just little part of function. I am able to move arrow constantly, but for better smooth movement of arrow, I am calling this function repeatedly with timer .01.

As I am doing both the processing together so its creating problem sometimes. Sometimes arrow movement method gets delayed and sometimes Line drwing method gets delayed.

Gnarled answered 31/8, 2013 at 12:8 Comment(0)
A
0

I would scrap the timer and do move it in touchesMoved.

My solution lets you draw on a UIImageView canvas and has a white box follow your finger while you're drawing. Drop it into any UIView to try it out:

// These should probably be @properties
static CGPoint lastPoint;
static CGPoint currentPoint;
static UIView *box;
static UIImageView *canvas;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch  = [touches anyObject];
    currentPoint    = [touch locationInView:self];
    lastPoint       = [touch locationInView:self];

    // Create the canvas the first time. Should probably do this elsewhere
    // but done here for paste-ability
    if (canvas == nil)
    {
        canvas = [[UIImageView alloc] initWithFrame:self.frame];
        canvas.backgroundColor = [UIColor redColor];
        [self addSubview:canvas];
    }

    // Create the box that follows the finger. Should probably do this elsewhere
    // but done here for paste-ability
    if (box == nil)
    {
        box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        box.backgroundColor = [UIColor whiteColor];
        [self addSubview:box];
    }

    // Ensure we can see it and move it right away
    box.alpha = 1.0f;
    box.center = CGPointMake(currentPoint.x, currentPoint.y - 50);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    currentPoint = [touch locationInView:self];

    // Set up everything for drawing
    UIGraphicsBeginImageContext(canvas.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [canvas.image drawInRect:CGRectMake(0, 0, canvas.image.size.width, canvas.image.size.height)];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 1);
    CGContextSetStrokeColorWithColor (context, [UIColor blueColor].CGColor);

    // Draw the path
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
    CGContextStrokePath(context);
    canvas.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Animate the box very quickly to the new location
    [UIView animateWithDuration:0.1f
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                        box.center = CGPointMake(box.center.x + (currentPoint.x - lastPoint.x),
                                                 box.center.y + (currentPoint.y - lastPoint.y));
                     }
                     completion:nil];

    // Remember our last touch
    lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Finish it off by fading out the box
    [UIView animateWithDuration:0.4f animations:^{
        box.alpha = 0.0f;
    }];
}
Acoustics answered 9/10, 2013 at 1:9 Comment(2)
It will draw the line with the finger move, But I need to move the arrow also at the same time on the line, So you dont think that they will concide with each other. For moving arrow, I am using timer, which is called every .01 seconds.Gnarled
Gotcha, original question didn't mention keeping the arrow on the line :) To do that just set the box.center to be equal to the lastPoint. If you don't want a slight animation you could just get rid of the animation block in touchesMoved:withEvent:.Acoustics

© 2022 - 2024 — McMap. All rights reserved.