简体   繁体   English

iOS - Gesture Recognizer translationInView

[英]iOS - Gesture Recognizer translationInView

I use the following line of code in a pan gesture recognizer: 我在平移手势识别器中使用以下代码行:

CGPoint translation = [sender translationInView:self.view];

If I move the associated processing to a long press gesture recognizer, there is not a translationInView method. 如果我将关联的处理移动到长按手势识别器,则没有translateInView方法。

My question is, how can I get the same value for translation if using the long-press recognizer? 我的问题是,如果使用长按识别器,如何获得相同的翻译价值?

Thanks 谢谢

CGPoint location = [recognizer locationInView:self.view];

For UILongPressgestureRecognizerv its not translation in view , it is locationInView . 对于UILongPressgestureRecognizerv,它不在视图中进行翻译,它是locationInView。

-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];

switch (recognizer.state) {
    case UIGestureRecognizerStateBegan:
        break;
    case UIGestureRecognizerStateChanged:
        break;
    case UIGestureRecognizerStateEnded:
        break;
    default:
        break;
    }   
}

Hope it will help you. 希望它会对你有所帮助。

Thanks for your reply. 感谢您的回复。 What I was really looking for was the computation for translationInView, which is different from locationInView. 我真正想要的是translateInView的计算,它与locationInView不同。 I resolved this with the following code: 我用以下代码解决了这个问题:

CGPoint location = [sender locationInView:self.view];
CGPoint translation;
translation.x = location.x - viewStartLocation.x;
translation.y = location.y - viewStartLocation.y;

It does require me to track the starting location, which I didn't have to do with the pan gesture recognizer, but it seems to work well. 它确实需要我跟踪起始位置,这与平移手势识别器没有关系,但它似乎运行良好。 The rest of my code is centered around the translation rather than the location, so I was trying to avoid having to rewrite that other code just for the sake of consistency. 我的其余代码以转换而不是位置为中心,所以我试图避免为了一致性而重写其他代码。

Thanks again for taking time to reply. 再次感谢您花时间回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM