我正在学习iOS开发,我在看UIGestureRecognizer
的。关于UIGestureRecognizers
我有一个看法。当您点击该视图时,我想要显示一个UIPopoverController
,我也希望它像UIButton
那样工作,因为它在按下时突出显示。
我想做到这一点的方法是使用2 UIGestureRecognizer
的 - 一个UITapGestureRecognizer
和UILongPressGestureRecognizer
我遇到的问题是亮点方法立即调用(我想),但如果我然后移动我的手指足够远,UITapGestureRecognizer
被取消。此时,我想调用另一种方法(unhighlight
)来恢复UIView
的初始背景颜色,但我在如何执行此操作时已丢失。
我对此很新,所以这个问题可能很基本,我很感谢任何人都可以给我的帮助。
在UIViewController
:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(togglePopover)];
[self.view addGestureRecognizer:tap];
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(highlight)];
press.minimumPressDuration = 0.f; //highlight immediately
press.delegate = self; //set the delegate to self
[self.view addGestureRecognizer:highlight];
//the delegate part of the UIViewController
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithOtherGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
return YES; //allows allow simultaneous recognition of gestures on this view
}
如果你希望视图像一个按钮,那为什么不使用'UIButton'? – rmaddy
好点,我当然可以(也可能会) - 我希望在提问这个问题的过程中,更多地了解一下'UIGestureRecognizer'。 – Adam
@rmaddy - 如果你让你的评论更像答案,并给出答案,我会接受它。你是对的,我应该只使用'UIButton' - 我已经这样做了,它正在按照我的需要工作。 – Adam