views:

38

answers:

0

I'm trying to create a view that a user can draw in, use multitouch to scale, and then draw some more. I have a basic view (canvas) where I add an imageView (sized to the canvase) and nil image. I draw in (on?) the image by using drawInRect. I copied my drawing code from here. This works fine.

I then added a multitouch check in touchesBegan and touchesMoved and if there are more than two fingers, I scale the image up or down using self.transform=CGAffineTransformMakeScale(scalefactor,scalefactor). This also seems to work fine.

But now I want the user to draw more on this scaled image. This does not work as currently coded. I can draw, but with a very weird effect where the drawing seems to be moving away from me and getts blurry.

I suspect I'm not using the cGAffineTransformMakeScale properly. I really have never understood tranforms. Perhaps I need to "commit" the tranform? Anyone have anythoughts?

- (void)awakeFromNib
{
      [self setMultipleTouchEnabled:YES];
    currentScale=1;
    mouseMoved = 0;
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = self.bounds;
    drawImage.backgroundColor=[UIColor greenColor];
    [self addSubview:drawImage];
    NSLog(@"initial image size:%f,%f",drawImage.image.size.width,drawImage.image.size.height);
    drawImage.autoresizingMask = UIViewAutoresizingNone;
}


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

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

    if ([touches count]>1) {

        NSLog(@"touches:%i",[touches count]);

        UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
        UITouch *touch2 = [[touches allObjects] objectAtIndex:1];

        initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self] 
                                                 toPoint:[touch2 locationInView:self]];


    }
    lastPoint = [touch locationInView:self];
    lastPoint.y -= 0;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"image:%f,%f",drawImage.image.size.width,drawImage.image.size.height);
    if ([touches count]==1) {


    mouseSwiped = YES;

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

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

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
    }else {

        UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
        UITouch *touch2 = [[touches allObjects] objectAtIndex:1];

        //Calculate the distance between the two fingers.
        CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
                                                       toPoint:[touch2 locationInView:self]];

        //Check if zoom in or zoom out.
        if(initialDistance > finalDistance) {
            currentScale=currentScale-0.01;
            if (currentScale<0.5){
                currentScale=0.5;
            }
            self.transform = CGAffineTransformMakeScale(currentScale, currentScale);
        }
        else {
            currentScale=currentScale+0.01;
            if (currentScale>1.5){
                currentScale=1.5;

            self.transform = CGAffineTransformMakeScale(currentScale, currentScale);
        }

    }
    }
}



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

    UITouch *touch = [touches anyObject];

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }



}

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {

    float x = toPoint.x - fromPoint.x;
    float y = toPoint.y - fromPoint.y;

    return sqrt(x * x + y * y);
}