1

我正在使用这些方法和这些变量

CGPoint touchBegan;
CGPoint touchEnd;

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

}

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

}

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

}

但我无法得到两点之间的距离。例如用手指画一条线并获取 CGPoint touchBegan 和 CGPoint touchEnd 之间的距离

感谢任何帮助谢谢

4

2 回答 2

5

它似乎不存在任何直接执行此操作的方法或函数,您必须取两点坐标的差异并使用勾股定理:

CGPoint touchBegan;
CGPoint touchEnd;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch= [touches anyObject];
    touchBegan= [touch locationInView: self.view];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch= [touches anyObject];
    touchEnd= [touch locationInView: self.view];
    CGFloat dx= touchBegan.x - touchEnd.x;
    CGFloat dy= touchBegan.y - touchEnd.y;
    CGFloat distance= sqrt(dx*dx + dy*dy);
    < Do stuff with distance >
}
于 2013-06-24T00:44:24.220 回答
2

只需实现您自己对勾股定理的演绎。例如:

CGPoint translation = CGPointMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
CGFloat distance = sqrtf(translation.x * translation.x + translation.y * translation.y);

或者,正如 Rob Mayoff 指出的那样,使用 Math.hhypotf方法更好:

CGFloat distance = hypotf(translation.x, translation.y);
于 2013-06-24T00:41:30.313 回答