Iphone 触摸移动,计算距离

Iphone 触摸移动,计算距离,iphone,objective-c,ios,Iphone,Objective C,Ios,当我拖动手指时,我将如何寻找距离。假设我的手指从A点开始,到B点,是10个像素,然后回到A点,总共是20个像素。我将如何计算?您可以跟踪用户移动的点。记住touchesBegin:中的点,在touchesMoved:中累积距离,并在touchesEnded:中获得整个距离 您可以跟踪用户移动的点。记住touchesBegin:中的点,在touchesMoved:中累积距离,并在touchesEnded:中获得整个距离 对于您的需求系统,请提供从设备屏幕获取触点的主要3种代表方法。这3种方法分别为

当我拖动手指时,我将如何寻找距离。假设我的手指从A点开始,到B点,是10个像素,然后回到A点,总共是20个像素。我将如何计算?

您可以跟踪用户移动的点。记住touchesBegin:中的点,在touchesMoved:中累积距离,并在touchesEnded:中获得整个距离


您可以跟踪用户移动的点。记住touchesBegin:中的点,在touchesMoved:中累积距离,并在touchesEnded:中获得整个距离


对于您的需求系统,请提供从设备屏幕获取触点的主要3种代表方法。这3种方法分别为

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@\"touchesBegan\");// here you can find out A point which user began to touch screen
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@\"touchesMoved\");// here get points of user move finger on screen to start to end point
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@\"touchesEnded\");// here user end touch or remove finger from Device Screen means  (B)
}

我希望这对您有用…

对于您的需求系统,请提供从设备屏幕获取触点的主要3种代理方法这3种方法是

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@\"touchesBegan\");// here you can find out A point which user began to touch screen
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@\"touchesMoved\");// here get points of user move finger on screen to start to end point
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@\"touchesEnded\");// here user end touch or remove finger from Device Screen means  (B)
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch=[touches anyObject];
point1 = [touch locationInView:self];//(point2 is type of CGPoint)
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch=[touches anyObject];
point2 = [touch locationInView:self];//(point2 is type of CGPoint)
}
我希望这对你有用

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch=[touches anyObject];
point1 = [touch locationInView:self];//(point2 is type of CGPoint)
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch=[touches anyObject];
point2 = [touch locationInView:self];//(point2 is type of CGPoint)
}
差分点x或y轴后,可以得到差分

点x或y轴差分后,您可以在Swift中获得差分

整个解决方案:

Swift的整体解决方案:


所有的答案到目前为止都不考虑多点触摸。如果您需要记住哪个手指对应于哪个拖动以支持多点触摸,那么这些解决方案将变得更加困难

如果你只是对delta感兴趣,比如说,你正在用两个手指同时滚动两个不同的东西,你可以直接从touchesMoved中的touch对象计算delta,忘记记住ToucheSStart中的任何状态


所有的答案到目前为止都不考虑多点触摸。如果您需要记住哪个手指对应于哪个拖动以支持多点触摸,那么这些解决方案将变得更加困难

如果你只是对delta感兴趣,比如说,你正在用两个手指同时滚动两个不同的东西,你可以直接从touchesMoved中的touch对象计算delta,忘记记住ToucheSStart中的任何状态

试试这个链接:在这里你可能会有一些想法试试这个链接:在这里你可能会有一些想法
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches {
        let a = t.location(in: view)
        let b = t.previousLocation(in: view)
        let delta = b.y - a.y
        doSomethingWithDelta()
    }
}
let nodesForFirstFinger = children.filter { $0.name == "firstGuys" }
let _ = nodesForFistFinger.map { $0.position.y += delta }