2015-02-08 90 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 
     }]; 
    } 
相關問題