Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use UIBezierPath to draw a line that follows a touch event?

I picked up the following piece of code from a similar question but I'm not having any luck getting it to work.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"Check #1");

    UITouch* touch = [touches anyObject];
    self.currentPath = [UIBezierPath bezierPath];
    currentPath.lineWidth = 3.0;
    [currentPath moveToPoint:[touch locationInView:self.view]];
    [paths addObject:self.currentPath];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self.currentPath addLineToPoint:[touch locationInView:self.view]];
    [self.view setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [[UIColor redColor] set];
    for (UIBezierPath *path in paths) {
        NSLog(@"Check #2?");
        [path stroke];
    }
}

What I've got from it is that I need to use UIBezierPath but I'm not sure how to get it to follow my finger. I only want to draw a line, start it when the user touches down and end it where they pick up their finger.

How can I achieve this line drawing using UIBezierPath?

like image 276
James Dunay Avatar asked Feb 23 '23 00:02

James Dunay


1 Answers

#import <UIKit/UIKit.h>

@interface Canvas : UIImageView

@property (nonatomic, assign) CGPoint location;

@end




#import "Canvas.h"

@implementation Canvas

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

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

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, self.location.x, self.location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.location = currentLocation;
}

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

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, self.location.x, self.location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.location = currentLocation;
}

@end
like image 52
bandejapaisa Avatar answered Apr 09 '23 02:04

bandejapaisa