找到两个CG点之间的距离?

Fab*_*bio 1 objective-c touch uitouch drawrect

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

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 {

}
Run Code Online (Sandbox Code Playgroud)

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

感谢任何帮助

Ram*_*uri 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 >
}
Run Code Online (Sandbox Code Playgroud)

  • `CGFloat 距离 = hypotf(dx, dy);` (8认同)