0

I'm using this code for the moving, scaling and rotating an UIImageView.

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

    CGPoint currentTouch1;
    CGPoint currentTouch2;
    NSArray *allTouches = [touches allObjects];
    UITouch* t;
    float scale,rotation;

    if([[event allTouches] count]==1){
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]))
        { 
            touch2=[t locationInView:nil];
            Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
            touch1=touch2;
        }
    }
    else if([[event allTouches] count]==2)
    {
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        currentTouch1=[t locationInView:nil];

        t=[[[event allTouches] allObjects] objectAtIndex:1];
        currentTouch2=[t locationInView:nil];


        scale = [self distance:currentTouch1 toPoint:currentTouch2] / [self distance:touch1 toPoint:touch2];
        rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
        if(isnan(scale)){
            scale=1.0f;
        }
        NSLog(@"rotation %f",rotation);

        NSLog(@"scale %f",scale);

        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]) &&
            CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:1] locationInView:self.view]))
        {

            Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
            Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);
        }
        else // In case of scaling or rotating the background imageView
        {
            imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
            imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
        }

        touch1=currentTouch1;
        touch2=currentTouch2;
    }
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
    return sqrt(fabs(point1.x - point2.x) + fabs(point1.y - point2.y));
}

However I get one error the whole time, Invalid operands to binary /. I get this error on the next line:

    scale = [self distance:currentTouch1 toPoint:currentTouch2] / [self distance:touch1 toPoint:touch2];
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tim
  • 13
  • 1
  • 9

1 Answers1

0

Have you declared distance:toPoint: in the .h files? If not, then the compiler (working strictly top to bottom with just a single pass) doesn't know what type the function returns and assumes something that doesn't work with the division operator.

So most likely, you can solve the problem by either declaring the function in the .h file or moving it before touchesMoved:withEvent:

Codo
  • 75,595
  • 17
  • 168
  • 206
  • I haven't declared it, and it must be that. Because when I put it in before touchesMoved it still doesn't work. How do I declare the distance:topoint than? Thanks in advanced for the answer! – Tim Apr 17 '11 at 18:03
  • If you put in before touchesMoved:withEvent: and it still doesn't work, then it must be something else and a declaration in the .h file won't. But if the error message has changed, then you have more than one problem. Is the error message still the same? – Codo Apr 17 '11 at 18:21
  • The error message changed, but then there are other things underclared, which are declared beneath the touchesMoved, like scale . Where do I have to put the line exactly? – Tim Apr 17 '11 at 19:56
  • But if I put it before, I get the error message that 'self' undeclared is. So couldn't it be something else ? – Tim Apr 18 '11 at 15:59