gpt4 book ai didi

objective-c - 同时使用 UILongPressGestureRecognizer 和 UISwipeGestureRecognizer

转载 作者:行者123 更新时间:2023-11-29 13:27:36 28 4
gpt4 key购买 nike

我使用长按手势创建了一个简单的 View (黑色方 block )并将其拖动到屏幕上 (UIGestureRecognizerStateChanged),直到我捕捉到 UIGestureRecognizerStateEnded。好的,这很好用。现在我希望能够开始识别这个黑色方形 View 的其他手势 (UISwipeGestureRecognizer)。算法很简单:

  1. 长按创建黑色方 block
  2. 手指停留在屏幕上
  3. 拖动正方形 View (setCenter:)
  4. 滑动并识别 UISwipeGestureRecognizer

问题是在长按手势未完成时无法识别滑动(UIGestureRecognizerStateEnded)

#pragma mark - View lifecycle

- (void)loadView {
[super loadView];

// Long press
self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[self.longPressGestureRecognizer setDelegate:self];
[self.view addGestureRecognizer:self.longPressGestureRecognizer];

// Swipe
self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[self.swipeGestureRecognizer setDelegate:self];
[self.swipeGestureRecognizer setEnabled:NO];
[self.view addGestureRecognizer:self.swipeGestureRecognizer];
}

#pragma mark - Gesture recognizer

- (void)gestureAction:(UIGestureRecognizer *)recognizer {

// Swipe
if ([recognizer isKindOfClass:NSClassFromString(@"UISwipeGestureRecognizer")]) {
NSLog(@"swipeGestureRecognizer handled!!");
}

// Long press
if ([recognizer isKindOfClass:NSClassFromString(@"UILongPressGestureRecognizer")]) {
if (recognizer.state == UIGestureRecognizerStateBegan) {
self.activeView = [[ItemView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.activeView setBackgroundColor:[UIColor blackColor]];
self.activeView.center = [recognizer locationInView:self.view];
[self.view addSubview:self.activeView];
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
if (![self.swipeGestureRecognizer isEnabled]) {
[self.swipeGestureRecognizer setEnabled:YES];
NSLog(@"swipeGestureRecognizer enabled");
}
self.activeView.center = [recognizer locationInView:self.view];
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
[self.swipeGestureRecognizer setEnabled:NO];
NSLog(@"swipeGestureRecognizer disabled");
[self.activeView removeFromSuperview];
self.activeView = nil;
}
}
}

#pragma mark - UIGestureRecognizerDelegate

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}

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

附言当您长按在日期 Canvas 上创建事件时,我希望其行为与 native 日历应用程序中的行为完全相同。如果您滑动某个对象(在结束长按手势之前),它会顺利消失。

最佳答案

要同时识别多个手势,您需要使用 UiGestureRecognizer 协议(protocol)中的以下委托(delegate)方法,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

这是 LINK到文档。

编辑

所以我尝试做同样的事情,是的,即使在我的情况下,直到长按结束才识别出滑动......!所以如果你想四处移动 View ,那么使用 PanGesture 而不是滑动手势..!它会工作我刚刚试过..:)

关于objective-c - 同时使用 UILongPressGestureRecognizer 和 UISwipeGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12722211/

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