2011-04-17 109 views
0

我使用此代码移动,缩放和旋转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)); 
} 

但是,我得到一个错误整个时间,无效的操作数二进制/。 我上下一行此错误:

scale = [self distance:currentTouch1 toPoint:currentTouch2]/[self distance:touch1 toPoint:touch2]; 
+0

移动它请出示'距离d的声明解决问题。 – kennytm 2011-04-28 18:18:04

回答

0

你声明的.h文件distance:toPoint:?如果不是这样,那么编译器(严格地从上到下只用一次传递)不知道函数返回的是什么类型,并假定某些不适用于除法运算符的东西。尖山:`

所以最有可能的,你可以通过声明函数在.h文件或之前touchesMoved:withEvent:

+0

我没有宣布它,它必须是。因为当我将它放入touchesMoved之前它仍然不起作用。我如何声明距离:topoint than?感谢高级的答案! – Tim 2011-04-17 18:03:12

+0

如果在touchesMoved之前放入:withEvent:并且它仍然不起作用,那么它必须是别的,而.h文件中的声明不会。但是如果错误信息已经改变,那么你有多个问题。错误消息是否仍然相同? – Codo 2011-04-17 18:21:24

+0

错误消息发生了变化,但是还有其他一些事情被隐瞒了,它们在touchesMoved下面声明,比如scale。我该在哪里准确地布置线​​路? – Tim 2011-04-17 19:56:28

相关问题