gpt4 book ai didi

ios - UIPanGestureRecognizer 无法按预期使用多个平移

转载 作者:行者123 更新时间:2023-11-29 04:11:01 28 4
gpt4 key购买 nike

本质上,我想做的是移动 View 以跟随用户的平移。只要使用相同的平移对象,这种方法就可以正常工作。当用户释放并启动另一个平移时,问题就出现了。

根据文档,translationInView 中的值是相对于平移开始处的位置。

所以我处理这个问题的策略是向我的 View 添加两个属性,这样我就可以判断是否正在使用同一个平移对象以及引用位置是什么。 self 对象是被移动的对象。它是 UIView 的子类。

   CGPoint originalPoint;
if (pan == self.panObject) {
//If the pan object is the same as the one in the property, use the saved value as the reference point.
originalPoint = CGPointMake(self.panStartLocation.x, self.panStartLocation.y);
} else {
//If the pan object is DIFFERENT, set the originalPoint from the existing center.
//self.center is in self.superview's coordinate system.
originalPoint = CGPointMake(self.center.x, self.center.y);
self.panStartLocation = CGPointMake(originalPoint.x, originalPoint.y);
self.panObject = pan;
}
CGPoint translation = [pan translationInView:self.superview];
self.center = CGPointMake(originalPoint.x+translation.x, originalPoint.y+translation.y);

这个方案不起作用,因为每个平移对象显然都是同一个对象。我花了一些时间在调试器中验证这一点,这似乎是正确的。我认为每次触摸时平移对象都会不同。既然这不起作用,那么还有什么替代方案呢?

最佳答案

我解决了。这是更正后的代码:

   CGPoint originalPoint;
if (pan.state == UIGestureRecognizerStateBegan) {
originalPoint = CGPointMake(self.center.x, self.center.y);
self.panStartLocation = CGPointMake(originalPoint.x, originalPoint.y);
} else {
originalPoint = CGPointMake(self.panStartLocation.x, self.panStartLocation.y);
}
CGPoint translation = [pan translationInView:self.superview];
self.center = CGPointMake(originalPoint.x+translation.x, originalPoint.y+translation.y);

编辑:更好的方法是利用手势识别器允许您设置翻译的事实:

[sender setTranslation:CGPointMake(0.0, 0.0) inView:self.pieceBeingMoved];

当您移动项目时执行此操作,然后下次新的翻译将相对于您刚刚移动到的位置。

关于ios - UIPanGestureRecognizer 无法按预期使用多个平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14410557/

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