iOS使用Pan Gesture Recognizer添加动画

时间:2015-02-08 18:04:59

标签: ios objective-c animation uigesturerecognizer

我试图在平移手势识别器中制作动画。但是,当识别器到达我在屏幕上的所需位置时,将调用UIStateGestureRecognizerStateEnded,并且我的动画不会被正确触发。这就是我要做的事情。我怎样才能使我的动画流畅,并且不会破坏手势识别器的状态。

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x, recognizer.view.center.y + translation.y);
    self.lineView1.center = CGPointMake(self.lineView1.center.x, recognizer.view.center.y + translation.y - 337.5f);
    self.lineView2.center = CGPointMake(self.lineView2.center.x, recognizer.view.center.y +translation.y + 337.5f);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

    //THIS CODE IS NOT ALLOWING THE ANIMATION TO RUN SMOOTHLY? AND ENDS THE GESTURE'S STATE. 
    if ((recognizer.view.center.y + translation.y)>300 && (recognizer.view.center.y + translation.y)<345) {

        [UIView animateWithDuration:3.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.joinHorzConst.constant= 60;
            self.joinLabel.alpha = 1;
        } completion:^(BOOL finished) {
            //completion
        }];
    }



    if (recognizer.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             recognizer.view.center = CGPointMake(recognizer.view.center.x, 121.5f);
                             self.lineView1.center = CGPointMake(281, -216);
                             self.lineView2.center = CGPointMake(281, 459);
                         } completion:^(BOOL finished) {

                             [UIView animateWithDuration:.2 animations:^{
                                 recognizer.view.center = CGPointMake(recognizer.view.center.x, 147.5f);
                                 self.lineView1.center = CGPointMake(281, -189);
                                 self.lineView2.center = CGPointMake(281, 486);                             } completion:^(BOOL finished) {
                                     [UIView animateWithDuration:.1 animations:^{
                                         recognizer.view.center = CGPointMake(recognizer.view.center.x, 141.5f);
                                         self.lineView1.center = CGPointMake(281, -196);
                                         self.lineView2.center = CGPointMake(281, 479);

                                     }];
                                 }];

                         }];
    }
}

提前致谢!

编辑:我想让我的动画工作正常:

用户在y轴上上下拖动识别器,就像滑块一样。当滑块按钮到达某些y轴(300 - 345)内时,我希望在我的一个标签上有一个动画,它只是将它向左推一点并将alpha向上转动。

它现在如何运作:

现在,滑块工作正常,直到达到300-345之间的y轴,此时我的gestureReconizerStatedidEnd被调用,滑块返回到原始位置并带有所需的动画。我不希望这种情况发生。我希望滑块保持自由移动,但标签动画就像滑块在300-345维度内一样。

1 个答案:

答案 0 :(得分:0)

我用限制设置标签x值的方式似乎扰乱了手势?不确定。但现在它是如何运作的。

  if ((recognizer.view.center.y + translation.y)>300 && (recognizer.view.center.y + translation.y)<345) {

        [UIView animateWithDuration:.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            [self.joinLabel setCenter:CGPointMake(183, 324)];
            self.joinLabel.alpha = 1;
        } completion:^(BOOL finished) {
            //completion
        }];
    }