“This is the 17th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”.

preface

In the previous article, the requirement was basically realized by customizing ScrollActivity, but since DragActivity was not handled, DragActivity and custom heart ScrollActivity were in a conflict state in gesture.

It may not seem like a big deal just from the UI presentation level, but it is a problem;

design

Since there is a conflict with DARG, I can just move all the processing to Drag.

Looking back, there wasn’t much to do:

  • Create AnimationController;
  • Start and stop at the right time;
  • Assign events to the Drag controller and AnimationController, respectively, as appropriate;

In ScrollDragController, there are only three parts involved in event processing: Update, end, cancel;

The update method DragUpdateDetails entry contains the gesture location information, so the desired gesture location is also known. It is not a problem to start the animation. Just place the method distribution here.

And the other two methods: end and cancel, as well as dispose, don’t forget to add the cancellation of animation;

implementation

  • First comes the update method:

    Due to the need to deal with the gesture, only from left to right to slide, call up the event of the previous page, so if you slide from right to left, directly super processing can be;

    Animation only needs to be started once. After execution, the position of the animation must be the position of the gesture, and the rest can be handed over to super.

    Also remember to update the target location, so that the required functionality is basically achieved:

    According to this idea, a simple implementation:

    For the AnimationController section, there is a small optimization point: When calculating the target value of the AnimateTo method, we can calculate the direction based on the current position and the target position, and then modify the target value passed to the AnimateTo. This way, we can display the animation to the gesture position regardless of whether the current position is in front or behind the gesture position.

    Of course, the boundary judgment in the TICK method is judged according to the direction as above.

    When calTargetDx computes the current target value, note that if the current page value is an integer, the page value needs to be reduced by 1 to point the target to the previous page.

  • Cancel, end, dispose methods

    All it does is terminate the animation and stop the call:

  • The other parts

    Because of the animation, it is possible to update position information even if the drag event is not called;

    So add some interception judgment here, so that no notification is sent during animation execution (maybe the event should be converted to the detail of the notification? DragUpdateDetails, for example)

At the end

Now that you’ve separated the gestures, set PageScrollPhysics() to look at the effect:

One last little point:

On millet reading, gestures will not be accepted or activated until the recovery animation ends after releasing the finger.

Now, gestures work even before the animation ends, interrupting the recovery animation; Then to achieve this effect, basically there is no problem