2015-02-08 250 views
0

我正在尝试使平移手势识别器内的动画。但是,当识别器到达屏幕上我想要的位置时,UIStateGestureRecognizerStateEnded会被调用,并且我的动画不会被正确触发。这是我如何去做的。我该如何做到这一点,以便我的动画平滑并且不会中断手势识别器的状态。iOS使用平移手势识别器添加动画

-(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尺寸范围内一样。

+0

我不确定我明白你想要做什么,但是不能在状态为“Chaged”时连续设置位置? – Linuxios 2015-02-08 18:09:38

+0

我编辑了我的问题,以更清晰地说明我希望动画如何工作。 @Linuxios – Andy 2015-02-08 18:16:48

回答

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 
     }]; 
    }