未调用 UISwipeGestureRecognizer UIGestureRecognizerStateChanged
UISwipeGestureRecognizer UIGestureRecognizerStateChanged not called
我在我的应用程序中定义了两个 UISwipeGestureRecognizer
:
UISwipeGestureRecognizer *swipeLeftVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
swipeLeftVolume.direction = UISwipeGestureRecognizerDirectionLeft;
[self.playerView addGestureRecognizer:swipeLeftVolume];
UISwipeGestureRecognizer *swipeRightVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
swipeRightVolume.direction = UISwipeGestureRecognizerDirectionRight;
[self.playerView addGestureRecognizer:swipeRightVolume];
在目标方法中我有 3 个状态:
UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
UIGestureRecognizerStateEnded
我注意到只有 UIGestureRecognizerStateEnded
被调用了。
知道问题出在哪里吗?我想识别特定 UIView
上的滑动:
- (void)handleSwipeGestureVolume:(UISwipeGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"Start");
} else if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"Changed");
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Finish");
}
}
确保为 self.playerView.
启用 UserInteraction
对于任何手势识别器,必须启用用户交互才能工作。没有它 none 的手势将被触发,包括 touchBegan:
、touchMove:
等
所以你需要[self.playerView setUserInteractionEnabled:TRUE];
还有一件事我想让你知道,如果你实现了 touchBegan:
、touchMove:
等,那么该方法将首先针对手势触发,而不是 UIGestureRecognizer
盾牌。
您似乎对 UISwipeGestureRecognizer
和 UIPanGestureRecognizer
感到困惑。
UISwipeGestureRecognizer
将仅生成 UIGestureRecognizerStateEnded
状态 而 UIPanGestureRecognizer
具有您想要的 3 个状态。
如果您需要同时接收 UIGestureRecognizerStateBegan
、UIGestureRecognizerStateChanged
、UIGestureRecognizerStateEnded
,请改用 UIPanGestureRecognizer
。
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
[self.playerView addGestureRecognizer:panGesture];
我在我的应用程序中定义了两个 UISwipeGestureRecognizer
:
UISwipeGestureRecognizer *swipeLeftVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
swipeLeftVolume.direction = UISwipeGestureRecognizerDirectionLeft;
[self.playerView addGestureRecognizer:swipeLeftVolume];
UISwipeGestureRecognizer *swipeRightVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
swipeRightVolume.direction = UISwipeGestureRecognizerDirectionRight;
[self.playerView addGestureRecognizer:swipeRightVolume];
在目标方法中我有 3 个状态:
UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
UIGestureRecognizerStateEnded
我注意到只有 UIGestureRecognizerStateEnded
被调用了。
知道问题出在哪里吗?我想识别特定 UIView
上的滑动:
- (void)handleSwipeGestureVolume:(UISwipeGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"Start");
} else if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"Changed");
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Finish");
}
}
确保为 self.playerView.
启用 UserInteraction对于任何手势识别器,必须启用用户交互才能工作。没有它 none 的手势将被触发,包括 touchBegan:
、touchMove:
等
所以你需要[self.playerView setUserInteractionEnabled:TRUE];
还有一件事我想让你知道,如果你实现了 touchBegan:
、touchMove:
等,那么该方法将首先针对手势触发,而不是 UIGestureRecognizer
盾牌。
您似乎对 UISwipeGestureRecognizer
和 UIPanGestureRecognizer
感到困惑。
UISwipeGestureRecognizer
将仅生成 UIGestureRecognizerStateEnded
状态 而 UIPanGestureRecognizer
具有您想要的 3 个状态。
如果您需要同时接收 UIGestureRecognizerStateBegan
、UIGestureRecognizerStateChanged
、UIGestureRecognizerStateEnded
,请改用 UIPanGestureRecognizer
。
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
[self.playerView addGestureRecognizer:panGesture];