iphone拖放奇怪的跳跃运动

时间:2022-07-16 09:44:58

I'm building an interactive anagram feature at the moment for my iPad app and am trying to use touchmove events to get the selected item to follow the movement smoothly, but it is jumping for some weird reason.

我正在为我的iPad应用程序构建一个交互式anagram功能,并且我正在尝试使用touchmove事件来使所选项目顺利地跟随运动,但它出现了一些奇怪的原因。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];

    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        NSLog(@"start moving");
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];
    CGPoint location = [touch locationInView:touch.view];
    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        NSLog(@"touch view centre (x,y) - (%f,%f)",touch.view.center.x,touch.view.center.y);
        NSLog(@"location (x,y) - (%f,%f)",location.x,location.y);
        touch.view.center = location;
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];

    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]){
        NSLog(@"Now stop moving!");
    }
}

The above are my 3 touch methods and it would seem like they should be working fine. You can see in the touchesMoved method that I am checking to see whether the object being moved is of a specific class (a custom class I made for the same project). Has anyone any ideas why this might be happening? It is following the cursor in the simulator but it seems to snap to the bottom corner before it follows the cursor, so it is basically jumping all over the screen.

以上是我的3种触摸方法,看起来它们应该可以正常工作。您可以在touchesMoved方法中看到我正在检查要查看的对象是否属于特定类(我为同一个项目创建的自定义类)。有没有人知道为什么会这样?它跟随光标在模拟器中,但它似乎在跟随光标之前捕捉到底角,所以它基本上在整个屏幕上跳跃。

Any ideas?

1 个解决方案

#1


1  

I believe the problem is that you are working with the touch relative to the subview being touched.

我相信问题是你正在处理相对于被触摸的子视图的触摸。

[touch locationInView:[touch.view superview]];

will get you a location in reference to the your custom view's parent.

将为您提供一个参考您的自定义视图的父级的位置。

In the touchesMoved method you can look at the displacement and apply the same displacement to your view

在touchesMoved方法中,您可以查看位移并将相同的位移应用于视图

if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        UITouch * touch = [touches anyObject];
        CGPoint previous = [touch previousLocationInView:[touch.view superview]];
        CGPoint current = [touch locationInView:[touch.view superview]];
        CGPoint displacement = CGPointMake(current.x - previous.x, current.y - previous.y);

        CGPoint center = touch.view.center;
        center.x += displacement.x;
        center.y += displacement.y;
        touch.view.center = center;
}

#1


1  

I believe the problem is that you are working with the touch relative to the subview being touched.

我相信问题是你正在处理相对于被触摸的子视图的触摸。

[touch locationInView:[touch.view superview]];

will get you a location in reference to the your custom view's parent.

将为您提供一个参考您的自定义视图的父级的位置。

In the touchesMoved method you can look at the displacement and apply the same displacement to your view

在touchesMoved方法中,您可以查看位移并将相同的位移应用于视图

if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        UITouch * touch = [touches anyObject];
        CGPoint previous = [touch previousLocationInView:[touch.view superview]];
        CGPoint current = [touch locationInView:[touch.view superview]];
        CGPoint displacement = CGPointMake(current.x - previous.x, current.y - previous.y);

        CGPoint center = touch.view.center;
        center.x += displacement.x;
        center.y += displacement.y;
        touch.view.center = center;
}