我有以下问题。
我正在使用 UILongPressGestureRecognizer
将 UIView 置于“切换模式”。如果 UIView
处于“切换模式”,用户可以在屏幕上拖动 UIView。为了在屏幕上拖动 UIView,我使用了方法 touchesBegan
、touchesMoved
和 touchesEnded
。
它有效,但是:我必须抬起手指才能拖动它,因为 touchesBegan
方法已经被调用,因此不会再次调用,因此我无法拖动 UIView
环绕屏幕。
在 UILongPressGestureRecognizer
被触发后,有没有办法手动调用 touchesBegan
(UILongPressGestureRecognizer
改变了一个 BOOL 值并且 touchesBegan
仅当此 BOOL 设置为 YES 时才有效。
最佳答案
UILongPressGestureRecognizer
是一个连续的手势识别器,因此与其求助于 touchesMoved
或 UIPanGestureRecognizer
,不如检查 UIGestureRecognizerStateChanged
,例如:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gesture];
}
- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
// user held down their finger on the screen
// gesture started, entering the "toggle mode"
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
// user did not lift finger, but now proceeded to move finger
// do here whatever you wanted to do in the touchesMoved
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
// user lifted their finger
// all done, leaving the "toggle mode"
}
}
关于ios - pressGestureRecognizer 和 touchesBegan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730056/