按下手势识别器和touchesBegan

3
我有以下问题。我正在使用UILongPressGestureRecognizer将UIView置于“切换模式”中。如果UIView处于“切换模式”,用户可以将UIView拖动到屏幕上。为了在屏幕上拖动UIView,我使用的方法是touchesBegantouchesMovedtouchesEnded
它确实起作用,但是:我必须抬起手指才能拖动它,因为touchesBegan方法已经被调用,因此不会再次调用,因此我无法将UIView拖动到屏幕上。
是否有任何方式在触发UILongPressGestureRecognizer后手动调用touchesBeganUILongPressGestureRecognizer更改BOOL值,并且只有当该BOOL设置为YES时才能使用touchesBegan)?

2个回答

10

UILongPressGestureRecognizer 是一种连续手势识别器,因此不需要使用 touchesMovedUIPanGestureRecognizer ,只需检查 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"
    }
}

我已经寻找这个答案很多个晚上了,先生。我尝试了我所知道的一切来使touchesMoved在长按过程中触发...但它根本不起作用。检查状态.Changed就解决了问题。非常感谢,@Rob。 - Clay Ellis

0
我建议您使用UIPanGestureRecognizer,因为它是拖动的推荐手势。
您可以使用以下属性来配置平移所需的最小触摸数和最大触摸数: maximumNumberOfTouches minimumNumberOfTouches 您可以处理像Began、Changed和Ended这样的状态,例如为所需的状态提供动画。
使用下面的方法将点转换为您想要的UIView。 - (void)setTranslation:(CGPoint)translation inView:(UIView *)view 例如: 1. 您必须使用全局变量来保留旧框架。在UIGestureRecognizerStateBegan中获得此变量。 2. 当状态为UIGestureRecognizerStateChanged时。您可以使用
-(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{
if(panGesture.state==UIGestureRecognizerStateBegan){ //retain the original state }else if(panGesture.state==UIGestureRecognizerStateChanged){ CGPoint translatedPoint=[panGesture translationInView:self.view]; //here you manage to get your new drag points. } }
拖动的速度。根据速度,您可以提供动画以显示UIView的弹跳。 - (CGPoint)velocityInView:(UIView *)view

谢谢您的回答,但是我如何获取翻译的坐标? - c2programming

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接