سؤال

I am trying to have 3 points connected depending on the last tap. For example, the user taps once, it makes a dot, the user taps again, it draws a line from tap 1 to tap 2. Finally, the user taps again, and it makes a line from tap 2 to tap 3. If the user moves his finger while tapping, it won't draw anymore, just move the dot that was created. I am trying to begin with just 2 points, but it won't draw, period. Here is what I am trying:

uiviewSubclass.h

@property (nonatomic) CGPoint firstPoint;
@property (nonatomic) CGPoint secondPoint;
@property (nonatomic) CGPoint thirdPoint;

uiviewSubclass.m

@implementation uiviewSubclass
{
    UIBezierPath *path;
    UIImage *incrementalImage; 
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:NO];
        [self setBackgroundColor:[UIColor clearColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:5.0];

        self.firstPoint = CGPointZero;
        self.secondPoint = CGPointZero;
        self.thirdPoint = CGPointZero;

    }
    return self;
}

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

        if(CGPointEqualToPoint(self.firstPoint, CGPointZero)){
            UITouch *touch = [touches anyObject];
            self.firstPoint = [touch locationInView:self];
            [path moveToPoint:self.firstPoint];
            self.hasSignature = @"YES";
        }
        else if(!(CGPointEqualToPoint(self.firstPoint, CGPointZero)) && CGPointEqualToPoint(self.secondPoint, CGPointZero)){
            UITouch *touch = [touches anyObject];
            self.secondPoint = [touch locationInView:self];
            [path moveToPoint:self.secondPoint];
            self.hasSignature = @"YES";
        }
    }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        if(CGPointEqualToPoint(self.firstPoint, CGPointZero)){
        UITouch *touch = [touches anyObject];
        self.firstPoint = [touch locationInView:self];
        [path moveToPoint:self.firstPoint];
        [self setNeedsDisplay];
        }
        else if(!(CGPointEqualToPoint(self.firstPoint, CGPointZero)) && CGPointEqualToPoint(self.secondPoint, CGPointZero)){
            UITouch *touch = [touches anyObject];
            self.secondPoint = [touch locationInView:self];
            [path addLineToPoint:self.secondPoint];
            [self setNeedsDisplay];
        }

    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *){
        [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [incrementalImage drawInRect:rect]; // (3)
    [[UIColor redColor] setStroke];
    [path stroke];
}

Any ideas why it isn't working? I am new to drawRect stuff, I apologize for any beginner errors.

هل كانت مفيدة؟

المحلول

I'd suggest you use a UITapGestureRecognizer with your view. As each tap is recognized, update the path with each point. Call setNeedsDisplay each time you update the path.

Your drawRect: method looks correct. So the important thing is your touch logic for updating the path.

نصائح أخرى

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"white" ofType:@"png"]]];
        return;
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    currentPoint.y -= 20;

    NSLog(@"current Point is x: %d, y: %d",currentPoint.x,currentPoint.y);

    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top