Example #1
0
    void Panned(NSObject r)
    {
        var recognizer = r as UIPanGestureRecognizer;
        var touchPoint = recognizer.LocationInView(View);

        switch (recognizer.State)
        {
        case UIGestureRecognizerState.Began:
            originalTouchPoint = touchPoint;
            break;

        case UIGestureRecognizerState.Changed:
            var offset = touchPoint.Y - originalTouchPoint.Y;
            offset = offset > 0 ? NMath.Pow(offset, 0.7f) : -NMath.Pow(-offset, 0.7f);
            rubberView.Transform = CGAffineTransform.MakeTranslation(0, offset);
            break;

        case UIGestureRecognizerState.Ended:
        case UIGestureRecognizerState.Cancelled:
            var timingParameters = UISpringTiming.MakeTimingParameters(damping: 0.6f, response: .3f);
            var animator         = new UIViewPropertyAnimator(duration: 0, parameters: timingParameters);
            animator.AddAnimations(() => rubberView.Transform = CGAffineTransform.MakeIdentity());
            animator.Interruptible = true;
            animator.StartAnimation();
            break;

        default:
            break;
        }
    }
Example #2
0
    void AnimateToRest()
    {
        var timingParameters = UISpringTiming.MakeTimingParameters(damping: 0.4f, response: 0.2f);
        var animator         = new UIViewPropertyAnimator(duration: 0, parameters: timingParameters);

        animator.AddAnimations(() => {
            Transform       = CGAffineTransform.MakeScale(1, 1);
            BackgroundColor = isOn ? onColor : offColor;
        });
        animator.Interruptible = true;
        animator.StartAnimation();
    }
Example #3
0
    void AnimateView()
    {
        var timingParameters = UISpringTiming.MakeTimingParameters(damping: dampingRatio, response: frequencyResponse);

        animator = new UIViewPropertyAnimator(duration: 0, parameters: timingParameters);
        animator.AddAnimations(() => {
            var translation = View.Bounds.Width - 2 * margin - 80;

            springView.Transform = CGAffineTransform.MakeTranslation(translation, 0);
        });
        animator.AddCompletion((x) => {
            springView.Transform = CGAffineTransform.MakeIdentity();
            AnimateView();
        });
        animator.StartAnimation();
    }
Example #4
0
    void Panned(NSObject r)
    {
        var recognizer = r as UIPanGestureRecognizer;
        var touchPoint = recognizer.LocationInView(View);
        var velocity   = recognizer.VelocityInView(View);

        switch (recognizer.State)
        {
        case UIGestureRecognizerState.Began:
            originalTouchPoint = touchPoint;
            break;

        case UIGestureRecognizerState.Changed:
            var offset = touchPoint.Y - originalTouchPoint.Y;
            if (offset > 0)
            {
                offset = NMath.Pow(offset, 0.7f);
            }
            else if (offset < -verticalOffset * 2)
            {
                offset = -verticalOffset * 2 - NMath.Pow(-(offset + verticalOffset * 2f), 0.7f);
            }
            accelerationView.Transform = CGAffineTransform.MakeTranslation(0, offset);
            TrackPause(velocity.Y, offset);
            break;

        case UIGestureRecognizerState.Ended:
        case UIGestureRecognizerState.Cancelled:
            var timingParameters = UISpringTiming.MakeTimingParameters(damping: 0.8f, response: 0.3f);
            var animator         = new UIViewPropertyAnimator(0, timingParameters);
            animator.AddAnimations(() => {
                accelerationView.Transform = CGAffineTransform.MakeIdentity();
                pauseLabel.Alpha           = 0;
            });
            animator.Interruptible = true;
            animator.StartAnimation();
            hasPaused = false;
            break;

        default:
            break;
        }
    }
    private void StartAnimationIfNeeded()
    {
        if (animator.Running)
        {
            return;
        }

        var timingParameters = UISpringTiming.MakeTimingParameters(damping: 1, response: 0.4f);

        animator = new UIViewPropertyAnimator(0, timingParameters);
        animator.AddAnimations(() => {
            momentumView.Transform = isOpen ? closedTransform : CGAffineTransform.MakeIdentity();
        });
        animator.AddCompletion((position) => {
            if (position == UIViewAnimatingPosition.End)
            {
                isOpen = !isOpen;
            }
        });

        animator.StartAnimation();
    }
    void Panned(object r)
    {
        var recognizer = r as UIPanGestureRecognizer;

        switch (recognizer.State)
        {
        case UIGestureRecognizerState.Began:
            StartAnimationIfNeeded();
            animator.PauseAnimation();
            animationProgress = animator.FractionComplete;
            break;

        case UIGestureRecognizerState.Changed:
            var fraction = -recognizer.TranslationInView(momentumView).Y / closedTransform.y0;
            if (isOpen)
            {
                fraction *= -1;
            }

            if (animator.Reversed)
            {
                fraction *= -1;
            }

            animator.FractionComplete = fraction + animationProgress;
            break;

        case UIGestureRecognizerState.Ended:
        case UIGestureRecognizerState.Cancelled:
            var yVelocity   = recognizer.VelocityInView(momentumView).Y;
            var shouldClose = yVelocity > 0;             // todo: should use projection instead

            if (yVelocity == 0)
            {
                animator.ContinueAnimation(parameters: null, durationFactor: 0);
                break;
            }
            if (isOpen)
            {
                if (!shouldClose && !animator.Reversed)
                {
                    animator.Reversed = !animator.Reversed;
                }
                if (shouldClose && animator.Reversed)
                {
                    animator.Reversed = !animator.Reversed;
                }
            }
            else
            {
                if (shouldClose && !animator.Reversed)
                {
                    animator.Reversed = !animator.Reversed;
                }
                if (!shouldClose && animator.Reversed)
                {
                    animator.Reversed = !animator.Reversed;
                }
            }
            var fractionRemaining = 1 - animator.FractionComplete;

            var distanceRemaining = fractionRemaining * closedTransform.y0;

            if (distanceRemaining == 0)
            {
                animator.ContinueAnimation(null, 0);
                break;
            }

            var relativeVelocity  = NMath.Min(NMath.Abs(yVelocity) / distanceRemaining, 30);
            var timingParameters  = UISpringTiming.MakeTimingParameters(damping: 0.8f, response: 0.3f, initialVelocity: new CGVector(dx: relativeVelocity, dy: relativeVelocity));
            var preferredDuration = new UIViewPropertyAnimator(duration: 0, parameters: timingParameters).Duration;
            var durationFactor    = preferredDuration / animator.Duration;
            animator.ContinueAnimation(parameters: timingParameters, durationFactor: (nfloat)durationFactor);
            break;

        default:
            break;
        }
    }