UIViewPropertyAnimation PanGesture реализации - PullRequest
0 голосов
/ 25 сентября 2019

Привет всем, я все еще работаю с UIViewPropertyAnimation, чтобы анимировать UIViewController, чтобы получить эффект перетаскиваемой панели , такой как карты приложений iOS.

Все работает нормально, но мне нужноограничить действие PanGesture в UIGestureRecognizerStateEnded.

В двух словах, я бы хотел, чтобы панель расширялась, только если пользователь прокручивает ее до определенной высоты, в противном случае, я хочу, чтобы панель возвращаласьв начальную позицию ..

Это пример, который поможет вам лучше понять:

case  UIGestureRecognizerStateEnded: 

      if (translation.y > 20) // open panel

      else // return (With Animation) in default position

до сих пор я реализовал функции UIViewPropertyAnimationтаким образом:

//MARK: - UIViewPropertyAnimation init

-(void)animateTransitionIfNeeded:(InteractiveViewControllerState)state duration:(NSTimeInterval)duration {

    BOOL runningAnimationIsEmpty = _runningAnimations.count == 0;

    CGSize frameSize = self.view.frame.size;

    if (!runningAnimationIsEmpty) return;
    else {

        UIViewPropertyAnimator *animator = [[UIViewPropertyAnimator alloc] initWithDuration:duration dampingRatio:interactiveAnimationDamping animations:^{

            switch (state) {

                case InteractiveViewControllerStateExpansed: {

                    self.interactiveViewController.view.frame = CGRectMake(0, (frameSize.height - self.interactiveControllerHeight) + interactiveViewDampingOffset, frameSize.width, self.interactiveControllerHeight);

                }

                    break;

                case InteractiveViewControllerStateCollapsed: {

                    self.interactiveViewController.view.frame = CGRectMake(0, frameSize.height - interactiveViewVisibleArea, frameSize.width,  self.interactiveControllerHeight);
                }
                    break;

                default:
                    break;
            }
        }];


        [animator addCompletion:^(UIViewAnimatingPosition finalPosition) {

            self.interactiveStateIsExapanded = !self.interactiveStateIsExapanded;
            [self.runningAnimations removeAllObjects];
        }];


        UIViewPropertyAnimator *cornerRadiusAnimation = [[UIViewPropertyAnimator alloc] initWithDuration:duration curve:UIViewAnimationCurveEaseOut animations:^{

            switch (state) {
                case InteractiveViewControllerStateExpansed:
                    self.interactiveViewController.view.layer.cornerRadius = 15;
                    break;

                    case InteractiveViewControllerStateCollapsed:
                    self.interactiveViewController.view.layer.cornerRadius = 0;
                    break;

                default:
                    break;
            }
        }];


        UIViewPropertyAnimator *blurAnimation = [[UIViewPropertyAnimator alloc] initWithDuration:duration dampingRatio:1 animations:^{

            switch (state) {
                case InteractiveViewControllerStateExpansed:
                    self.blurVisualEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
                    break;

                    case InteractiveViewControllerStateCollapsed:
                    self.blurVisualEffectView.effect = nil;
                    break;

                default:
                    break;
            }
        }];

        [animator startAnimation];
        [cornerRadiusAnimation startAnimation];
        [blurAnimation startAnimation];

        [_runningAnimations addObject:animator];
        [_runningAnimations addObject:cornerRadiusAnimation];
        [_runningAnimations addObject:blurAnimation];
    }
}

#pragma mark - UIPanGesture - Draggable View Controller -

-(void)userDidDragPanel:(UIPanGestureRecognizer *)recognizer {

    switch (recognizer.state) {

        case UIGestureRecognizerStateBegan:
            [self startInteractiveTransition:self.interactiveState duration:interactiveAnimationDuration];

            break;

        case UIGestureRecognizerStateChanged: {

            CGPoint translation = [recognizer translationInView:self.interactiveViewController.view];
            CGFloat fraction = translation.y / _interactiveControllerHeight;

            fraction = self.interactiveStateIsExapanded ? fraction : -fraction;

            [self updateInteractiveTransition:fraction];

        }
            break;

        case  UIGestureRecognizerStateEnded: 
            [self continueInteractiveTransition:0];

        default:
            break;
    }
}

-(void)startInteractiveTransition:(InteractiveViewControllerState)state duration:(NSTimeInterval)duration {

    if (_runningAnimations.count == 0) [self animateTransitionIfNeeded:state duration:duration];

    for (UIViewPropertyAnimator *animator in _runningAnimations) {
         [animator pauseAnimation];
        _animationProgressWhenInterrupted = animator.fractionComplete;
    }
}

-(void)updateInteractiveTransition:(CGFloat)fractionComplete {

    for (UIViewPropertyAnimator *animator in _runningAnimations)
        animator.fractionComplete = fractionComplete + _animationProgressWhenInterrupted;
}

-(void)continueInteractiveTransition:(CGFloat)fraction{

    for (UIViewPropertyAnimator *animator in _runningAnimations)
        [animator continueAnimationWithTimingParameters:nil durationFactor:fraction];
}

Может кто-нибудь помочь мне решить эту проблему?Я надеюсь, что мне удалось понять себя ... по любым вопросам, которые я доступен

...