简体   繁体   English

手势识别器重置

[英]Pan Gesture Recognizer reset

I've added a pan gesture to my button, when i move it without any additional code, everything is well, but when i add some piece of code, which is commented in the example below, button starts reseting to its origin position. 我在按钮上添加了平移手势,当我在不添加任何其他代码的情况下移动它时,一切都很好,但是当我添加一些代码时(在下面的示例中进行了注释),按钮开始重置为其原点位置。

Why is this happening? 为什么会这样呢? What's the reason for this? 这是什么原因?

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
switch([recognizer state]){
    case  UIGestureRecognizerStateBegan: {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setFrame:recognizer.view.frame];
        [button setBackgroundColor: [UIColor redColor]];

//      [self.view insertSubview:button belowSubview:recognizer.view];
        _tempButton = button;

    }break;
    case  UIGestureRecognizerStateChanged: {

        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

    }break;
}
}

第一第二

The code that you have added at the UIGestureRecognizerStateChanged case, move it in the header of your Action Method: 您在UIGestureRecognizerStateChanged情况下添加的代码,将其移至Action方法的标题中:

CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

After that another thing for you to do is to make another Case in your Switch Method: 之后,您要做的另一件事是在Switch方法中创建另一个Case:

//This checks if you ended the pan gesture (removed finger from object)
case  UIGestureRecognizerStateEnded:
{
    CGPoint velocity = [recognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200;
    NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                 recognizer.view.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut     animations:^{
    recognizer.view.center = finalPoint;
    } completion:nil];
}

So what I did above in the UIGestureRecognizerStateEnded state is that, i took the final point where the user sent the image. 因此,我在UIGestureRecognizerStateEnded状态下所做的就是,我到达了用户发送图像的最后一个点。 After that, i set the center of the image equal to the point that the image was the last moment. 在那之后,我将图像的中心设置为等于图像是最后一刻的点。 So now the image wont return to the previous point but it will remain where the user will take it. 因此,现在图像将不会返回到上一个点,但它将保留在用户将要拍摄的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM