错误:“二进制操作数 / 无效”

发布于 2024-11-01 21:48:40 字数 2390 浏览 1 评论 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];

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2024-11-08 21:48:40

您是否在 .h 文件中声明了 distance:toPoint: ?如果不是,那么编译器(严格从上到下工作,只需一次传递)不知道函数返回什么类型,并假设某些不适用于除法运算符。

因此,您很可能可以通过在 .h 文件中声明该函数或将其移到 touchesMoved:withEvent: 之前来解决问题

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文