簡體   English   中英

iOS使用Pan Gesture Recognizer添加動畫

[英]iOS adding an animation with Pan Gesture Recognizer

我正在嘗試在平移手勢識別器中制作動畫。 但是,當識別器到達屏幕上我想要的位置時,將調用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調高。

現在如何運作:

現在,滑塊可以正常工作,直到我到達y軸(介於300-345之間)時,此時我的gestureReconizerStatedidEnd會被調用,並且滑塊會以所需的動畫返回到其原始位置。 我不希望這種情況發生。 我希望滑塊繼續自由移動,但是標簽動畫會發生,就像滑塊在300-345尺寸范圍內一樣。

我設置帶有約束的標簽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
        }];
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM