繁体   English   中英

ios UISwipeGestureRecognizer计算偏移量

[英]ios UISwipeGestureRecognizer calculate offset

我正在为我的应用程序添加滑动手势识别器

- (void)createGestureRecognizers
{

    //adding swipe up gesture
    UISwipeGestureRecognizer *swipeUpGesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpGesture:)];
    [swipeUpGesture setDirection:UISwipeGestureRecognizerDirectionUp];
    [self.view addGestureRecognizer:swipeUpGesture];
    [swipeUpGesture release];
}

以及处理滑动事件的方法:

-(IBAction)handleSwipeUpGesture:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"handleSwipeUpGesture: called");
}

我该如何计算偏移? 移动视图?

UISwipeGestureRecognizer用于检测离散滑动手势 - 它仅在滑动完成后触发您的动作一次 - 因此,如果您询问手指移动的偏移或距离,您可能希望查看创建UIGestureRecognizer的子类或使用UIPanGestureRecognizer可获取连续的手势信息。 不确定你究竟想要做什么,但是UIScrollView也可以按顺序排列......

查看手势识别器苹果文档

UISwipeGestureRecognizer的UIGestureRecognizer抽象超类有着愚蠢的方法

- (CGPoint)locationInView:(UIView *)view
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view

这允许您知道手势在视图中的位置,但这是一个离散的手势识别器(无论您想要调用它,您都无法控制,它会以给定的“平移”或“偏移”触发)。 听起来你正在寻找连续控制,为此你需要一个UIPanGestureRecognizer ,它有以下方法(为你做翻译计算)

- (CGPoint)translationInView:(UIView *)view
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
- (CGPoint)velocityInView:(UIView *)view

然后,当手势不断展开时,您将获得快速回调。

暂无
暂无

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

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