Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当长按时启用UIPangestureRecognitor_Ios_Objective C_Uigesturerecognizer - Fatal编程技术网

Ios 当长按时启用UIPangestureRecognitor

Ios 当长按时启用UIPangestureRecognitor,ios,objective-c,uigesturerecognizer,Ios,Objective C,Uigesturerecognizer,当customView没有longPress时,我想在customView上启用uipangestureerecognizer (我希望当您长按customView时,customView将切换到“移动模式”,您可以通过拖动来移动customView。) 但在此代码中,仅调用了longPressAction:泛操作:未调用。 如何修复它以启用PanAction: - (void)viewDidLoad { [self.view addSubview:customView]; U

customView
没有
longPress
时,我想在
customView
上启用
uipangestureerecognizer
(我希望当您长按
customView
时,
customView
将切换到“移动模式”,您可以通过拖动来移动
customView
。)

但在此代码中,仅调用了
longPressAction:
<代码>泛操作:未调用。
如何修复它以启用
PanAction:

- (void)viewDidLoad
{
    [self.view addSubview:customView];

    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [customView addGestureRecognizer:longPressRecognizer];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [customView addGestureRecognizer:panRecognizer];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)recognizer
{
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        CustomView *customView = (CustomView *)recognizer.view;
        customView.panRecongnizerEnabled = YES; //panRecongnizerEnabled is CustomView's property
    }
    if ([recognizer state] == UIGestureRecognizerStateEnded) {
        CustomView *customView = (CustomView *)recognizer.view;
        customView.panRecongnizerEnabled = NO;
    }
}

- (void)panAction:(UIPanGestureRecognizer *)recognizer
{
    CustomView *customView = (CustomView *)recognizer.view;
    if (customCell.panRecongnizerEnabled == NO) return;
    NSLog(@"running panAction");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

您的ViewController需要符合
UIgestureRecognitizerDelegate
。我怀疑您已经这样做了,否则
应该使用手势识别器同时识别
就没有任何意义了。但您肯定缺少的是将
手势识别器的委托设置到您的viewController:

longPressRecognizer.delegate = self;
panRecognizer.delegate = self;
现在您应该同时收到长按和平移


注意:我在没有任何customView的情况下进行了测试,只是将它们添加到了
self.view
。至少在这种情况下,上面的代码按预期工作。

我知道这个问题已经解决了一段时间,但我最近不得不做一些类似的事情,并且有一个更干净的解决方案。您只需使用
ui长按GestureRecognitor

长时间的新闻手势是连续的。手势开始了 (UIgestureRecognitizerStateStart)当允许的手指数 (numberOfTouchesRequired)已按指定时间 (minimumPressDuration)并且触摸不会超出 允许的移动范围(允许移动)。手势 每当手指移动时,识别器都会转换到更改状态, 当任何手指 被举起

因此,对于您需要的内容,不需要使用
uipangestrerecognizer
。只需添加
ui长按手势识别器
,然后将其附加到主视图

- (void)viewDidLoad {
    [super viewDidLoad];

    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    // set this to your desired delay before the pan action will start.
    longPressRecognizer.minimumPressDuration = 0.5; 
    [self.view addGestureRecognizer:longPressRecognizer];
}
然后,实施longPressAction方法,不仅查看
UIgestureRecognitizerStateStart
UIgestureRecognitizerStateEnded
状态,还查看
UIgestureRecognitizerStateChanged
状态:

- (void)longPressAction:(UIGestureRecognizer *)recognizer
{
    if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged ) {
        CGPoint touchPoint =  [recognizer locationInView:self.view];
        [self.viewToMove setCenter: touchPoint];
        NSLog(@"running panAction");
    }
}

在我的示例中,我只需移动一个虚拟视图来跟踪用户手指的中心,但您可以将任何逻辑放入
UIPangestureRecognitor
的代码中。只要把它放在if块中,它就大大简化了代码,而且不需要处理两个手势识别器之间的交互。当用户只是在屏幕上移动手指(但没有长按)时,您的代码还会导致对
panAction
方法的许多不必要的调用

您可以通过在长按后启用平移来完成此操作。无论如何,您必须保存一个标志以知道是否调用了longPress。为了在长按后启用平移手势,可以使用平移手势代理,如下所示:

let previewLongPress = UILongPressGestureRecognizer(target: self, action: #selector(onLongPressed(sender:)))
let previewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPanGesture(sender:)))

previewPanGesture?.delegate = self
您的代表应该是这样的:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return gestureRecognizer == previewPanGesture && otherGestureRecognizer == previewLongPress
}
现在,你可以在长按手势后做出平移手势


希望它能帮助其他人。

老实说,在长按之后,您应该在UIView上收听TouchsBegind/TouchsMoved/TouchsEnded,这样,您就可以设置锁定的边界并适当地移动视图。这有助于我了解为什么应该使用GestureRecognitor同时识别,而GestureRecognitor应该开始不触发。谢谢这太棒了!UILongPressGestureRecognitor不再使用此选项阻止滚动。