我用长按手势创建了一个简单的视图(黑色方块)并将其拖到屏幕上(UIGestureRecognizerStateChanged),直到我捕捉到 UIGestureRecognizerStateEnded。好的,这很好用。现在我希望能够开始识别这个黑色方形视图的附加手势(UISwipeGestureRecognizer)。算法很简单:
- 长按创建黑色方块
- 手指停留在屏幕上
- 拖动方形视图 (setCenter:)
- 滑动识别 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;
}
PS 当您通过长按在日画布上创建事件时,我希望其行为与本机日历应用程序中的行为完全相同。如果您滑动对象(在结束长按手势之前),它会平滑消失。