gpt4 book ai didi

ios - longPress 时启用 UIPanGestureRecognizer

转载 作者:可可西里 更新时间:2023-11-01 04:43:44 25 4
gpt4 key购买 nike

customView 执行 longPress 时,我想在 customView 上启用 UIPanGestureRecognizer
(我希望当你longPresscustomView时,customView会切换到“移动模式”,你可以移动customView通过拖动。)

但是在这段代码中,只有 longPressAction: 被调用了。 panAction: 没有调用。
如何修复它以启用 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 需要符合 UIGestureRecognizerDelegate。我怀疑您已经这样做了否则shouldRecognizeSimultaneouslyWithGestureRecognizer 将没有任何意义。但是您绝对缺少的是将 gestureRecognizer 的委托(delegate)设置为您的 viewController:

longPressRecognizer.delegate = self;
panRecognizer.delegate = self;

现在您应该同时接收到长按和平移。

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

关于ios - longPress 时启用 UIPanGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30014846/

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