当手势已经激活时如何启动手势识别器?
How to initiate a gesture recognizer when a gesture is already active?
我有一个子视图 A(self.thumbnailImageView) 添加到我的超级视图中。我已将 UILongPressGestureRecognizer 和 UISwipeGestureRecognizer 添加到我的子视图 A。
[self addLongPressGestureRecognizerForPreviewCell:self.thumbnailImageView];
[self addSwipeUpGestureRecognizerForImageView:self.thumbnailImageView];
现在在处理程序方法中,当 UILongPressGestureRecognizer 状态开始时,我将子视图 B(bigPreviewImage) 添加到我的超级视图 (self.view)。
-(void)tapGesture:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// Long press detected, start the timer
[self showPreviewImage:recognizer];
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
[self hidePreviewImage];
}
}
-(void)showPreviewImage:(UILongPressGestureRecognizer *)recognizer{
UIImageView *bigPreviewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Desktop"]];
bigPreviewImage.frame = CGRectMake(self.thumbnailImageView.frame.origin.x - 50.0, self.thumbnailImageView.frame.origin.y + self.thumbnailImageView.frame.size.height + 10.0, 300.0, 250.0);
bigPreviewImage.tag = 10000;//200.0 & 125.0
[bigPreviewImage setUserInteractionEnabled:YES];
[self.view addSubview:bigPreviewImage];
}
现在,一旦 UILongPressGestureRecognizer 处于活动状态并且用户仍用手指按住子视图 A,如果用户向上滑动屏幕,我希望启动 SwipeGestureRecognizer。但同样的事情并没有发生。如何在手势已激活时启动手势识别器?
我已经实现了shouldRecognizeSimultaneouslyWithGestureRecognizer
方法,但仍然没有调用向上滑动手势方法。如果我遗漏了什么,请告诉我。
知道了!!!
我们不需要单独的滑动手势识别器。 Long Press Gesture Recognizer 中的不同状态可以用来处理这种情况。
长按手势有不同的状态,如UIGestureRecognizerStateBegan
、UIGestureRecognizerStateChanged
和UIGestureRecognizerStateEnded
。
UIGestureRecognizerStateBegan
长按子视图后立即调用。
UIGestureRecognizerStateChanged
在用户尝试移动手指时被调用。
UIGestureRecognizerStateEnded
当用户从触摸点抬起手指时调用。
-(void)longPressGestureForPreviewImageView:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// Long press detected, start the timer
[self showPreviewImage:recognizer];
}
else if(recognizer.state == UIGestureRecognizerStateChanged)
{
NSLog(@"Swipe up");
if ([self.thumbnailImageView.gestureRecognizers containsObject:recognizer]) {
[self.thumbnailImageView removeGestureRecognizer:recognizer];
}
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
[self hidePreviewImage];
}
因此我们可以使用手势委托方法和长按手势识别器一起处理滑动。
我有一个子视图 A(self.thumbnailImageView) 添加到我的超级视图中。我已将 UILongPressGestureRecognizer 和 UISwipeGestureRecognizer 添加到我的子视图 A。
[self addLongPressGestureRecognizerForPreviewCell:self.thumbnailImageView];
[self addSwipeUpGestureRecognizerForImageView:self.thumbnailImageView];
现在在处理程序方法中,当 UILongPressGestureRecognizer 状态开始时,我将子视图 B(bigPreviewImage) 添加到我的超级视图 (self.view)。
-(void)tapGesture:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// Long press detected, start the timer
[self showPreviewImage:recognizer];
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
[self hidePreviewImage];
}
}
-(void)showPreviewImage:(UILongPressGestureRecognizer *)recognizer{
UIImageView *bigPreviewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Desktop"]];
bigPreviewImage.frame = CGRectMake(self.thumbnailImageView.frame.origin.x - 50.0, self.thumbnailImageView.frame.origin.y + self.thumbnailImageView.frame.size.height + 10.0, 300.0, 250.0);
bigPreviewImage.tag = 10000;//200.0 & 125.0
[bigPreviewImage setUserInteractionEnabled:YES];
[self.view addSubview:bigPreviewImage];
}
现在,一旦 UILongPressGestureRecognizer 处于活动状态并且用户仍用手指按住子视图 A,如果用户向上滑动屏幕,我希望启动 SwipeGestureRecognizer。但同样的事情并没有发生。如何在手势已激活时启动手势识别器?
我已经实现了shouldRecognizeSimultaneouslyWithGestureRecognizer
方法,但仍然没有调用向上滑动手势方法。如果我遗漏了什么,请告诉我。
知道了!!!
我们不需要单独的滑动手势识别器。 Long Press Gesture Recognizer 中的不同状态可以用来处理这种情况。
长按手势有不同的状态,如UIGestureRecognizerStateBegan
、UIGestureRecognizerStateChanged
和UIGestureRecognizerStateEnded
。
UIGestureRecognizerStateBegan
长按子视图后立即调用。UIGestureRecognizerStateChanged
在用户尝试移动手指时被调用。UIGestureRecognizerStateEnded
当用户从触摸点抬起手指时调用。
-(void)longPressGestureForPreviewImageView:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// Long press detected, start the timer
[self showPreviewImage:recognizer];
}
else if(recognizer.state == UIGestureRecognizerStateChanged)
{
NSLog(@"Swipe up");
if ([self.thumbnailImageView.gestureRecognizers containsObject:recognizer]) {
[self.thumbnailImageView removeGestureRecognizer:recognizer];
}
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
[self hidePreviewImage];
}
因此我们可以使用手势委托方法和长按手势识别器一起处理滑动。