UIGestureRecognizer 滑动并按住
我正在尝试实现滚动菜单的效果。如果用户进行单次滑动(向任何方向),我将为每次滑动移动一个项目。使用 UISwipeGestureReconizer 效果很好,但是如果用户在滑动后继续按住屏幕上的手指,我想继续滚动直到手指抬起。
看来我想将 UISwipeGestureRecognizer 与 UILongPressGestureRecognizer 结合起来来完成此任务。成功滑动 (UISwipeGestureRecognizer
) 后,启用 UILongPressGestureRecognizer
并开始滚动,直到手指抬起(UILongPressGestureRecognizer
结束)。
问题是成功滑动后不会发生 UILongPressGestureRecognizer
事件。
下面是一个示例:
// Create single swipe recognizer to recognize right swipes
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleSwipeFrom:)];
swipeRecognizer.delegate = self;
[_gestureView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
// Create long press recognizer to recognize right press and holds
longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAndHoldFrom:)];
longPressRecognizer.delegate = self;
[longPressRecognizer setMinimumPressDuration:0.5];
[longPressRecognizer setEnabled:NO];
[_gestureView addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
- (void)handleSingleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if ([recognizer state] == UIGestureRecognizerStateEnded) {
// Move one item in direction based on recognizer.direction
[_longPressGestureRecognizer setEnabled:YES];
}
}
如果我在滑动后将手指放在屏幕上,则永远不会调用 UILongPressGestureRecognizer 事件。关于如何实现这一目标有什么想法吗?
I am trying to implement the effect of scrolling through a menu. If the user does a single swipe (in any direction) I will move one item for each swipe. This is working just fine using UISwipeGestureReconizer
, but if the user keeps holding there finger down on the screen after the swipe I want to keep scrolling until the finger has lifted.
It seems like I would want to combine UISwipeGestureRecognizer
with UILongPressGestureRecognizer
to accomplish this. After a successful swipe (UISwipeGestureRecognizer
), enable the UILongPressGestureRecognizer
and start scrolling until a finger has lifted (UILongPressGestureRecognizer
ends).
The problem is that UILongPressGestureRecognizer
event does not occur after a successful swipe.
Heres an example:
// Create single swipe recognizer to recognize right swipes
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleSwipeFrom:)];
swipeRecognizer.delegate = self;
[_gestureView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
// Create long press recognizer to recognize right press and holds
longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAndHoldFrom:)];
longPressRecognizer.delegate = self;
[longPressRecognizer setMinimumPressDuration:0.5];
[longPressRecognizer setEnabled:NO];
[_gestureView addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
- (void)handleSingleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if ([recognizer state] == UIGestureRecognizerStateEnded) {
// Move one item in direction based on recognizer.direction
[_longPressGestureRecognizer setEnabled:YES];
}
}
If I keep my finger on the screen after the swipe, the UILongPressGestureRecognizer event is never called. Any ideas on how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试实现 UIGestureRecognizerDelegate 并使用此方法:
这将允许两者同时运行。您需要将代码添加到手柄滑动并保持方法中,以确保它仅在滑动完成后运行。
干杯
Try implementing the UIGestureRecognizerDelegate and use this method:
This will allow the two to run simultaneously. You will need to add code into the handle swipe and hold method though in order to make sure it is only being run after the swipe is completed.
Cheers