gpt4 book ai didi

ios - 由于设置了 UIPanGestureRecognizer,未调用 UISwipeGestureRecognizer @selector

转载 作者:行者123 更新时间:2023-11-28 18:34:13 24 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我进行了以下设置:

- (void)setupGestures
{
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panRecognizer];

UISwipeGestureRecognizer* swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

[swipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:swipeUpRecognizer];
}

// then I have following implementation of selectors

// this method supposed to give me the length of the swipe

- (void)panGesture:(UIPanGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
startLocation = [sender locationInView:self.view];
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint stopLocation = [sender locationInView:self.view];
CGFloat dx = stopLocation.x - startLocation.x;
CGFloat dy = stopLocation.y - startLocation.y;
CGFloat distance = sqrt(dx*dx + dy*dy );
NSLog(@"Distance: %f", distance);
}
}

// this method does all other actions related to swipes

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
UISwipeGestureRecognizerDirection direction = [gestureRecognizer direction];
CGPoint touchLocation = [gestureRecognizer locationInView:playerLayerView];

if (direction == UISwipeGestureRecognizerDirectionDown && touchLocation.y > (playerLayerView.frame.size.height * .5))
{
if (![toolbar isHidden])
{
if (selectedSegmentIndex != UISegmentedControlNoSegment)
{
[self dismissBottomPanel];
}
else
{
[self dismissToolbar];
}
}
}
}

所以问题是 handleSwipe 永远不会被调用...当我注释掉 UIPanGestureRecognizer 设置时,handleSwipe 开始工作。

我对手势识别编程还很陌生,所以我假设我在这里遗漏了一些基本知识。

非常感谢任何形式的帮助!

最佳答案

您需要告诉手势如何相互交互。这可以通过让它们同时运行(默认情况下它们不会运行)或将一个设置为仅在另一个失败时才运行来实现。

为了让它们都起作用,让你的类成为手势的委托(delegate)并实现

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

返回YES

要将一个设置为仅在另一个失败时才工作,请使用 requireGestureRecognizerToFail:

关于ios - 由于设置了 UIPanGestureRecognizer,未调用 UISwipeGestureRecognizer @selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22055625/

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