gpt4 book ai didi

ios - UIPanGestureRecognizer 获取以像素为单位的滑动距离

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:43 24 4
gpt4 key购买 nike

我正在尝试使用 UIPanGestureRecognizer 跟踪用户滑动的水平距离,但我很难解释结果。这是我正在使用的代码:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

CGPoint startLocation;

if (recognizer.state == UIGestureRecognizerStateBegan) {
startLocation = [recognizer locationInView:self.view];
}

if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint stopLocation = [recognizer locationInView:self.view];
CGFloat dx = stopLocation.x - startLocation.x;
NSLog(@"dx: %f", dx);
}
}

如果我从左向右滑动,我会得到如下输出:

dx: 50328911327402404790403072.000000

我的屏幕只有 320 像素宽,所以我的最终结果不能大于 320。我的代码有问题还是我只是错误地解释了这个数字?我怎样才能得到这个以像素为单位的值?谢谢!

最佳答案

startLocationhandlePan: 返回后不会持续存在,所以你得到了垃圾。

startLocation 声明为 static 或将其保存在实例变量/属性中。

关于ios - UIPanGestureRecognizer 获取以像素为单位的滑动距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27520787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com