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

preface

In the previous article, Item was automatically positioned to the position where the gesture was pressed, but there was still a problem:

If the gesture is moved before the animation ends, the animation does not track the gesture’s latest position, as shown in the figure below:

Let’s deal with this now:

Analysis of the

  • The reason for this problem is that the animation does not make judgments based on the latest gesture information; So if you want to make a judgment based on the latest gesture information, the first thing to do is to pass in the latest gesture;

  • Passed in gesture information, but also according to the gesture information, terminate or extend the animation;

To expand, the content required by the above two points can be summarized as follows:

  • Modify the animteTo method so that it no longer rebuilds the AnimationController all the time and passes in the latest gesture information without affecting animation playback;

  • For gestures that press left and then swipe right, extend the animation until the finger position is tracked;

  • For gestures that press right and then swipe left, the animation should stop early when the finger position is reached;

plan

  • First, pass in the gesture information, which basically means that while the activity is running, just update the destination position. For simple and quick verification, you can do this:

    Create a new method to update the target location:

    The animateTo method then determines that the current Activity is a new custom Activity and calls the update method:

  • As for the processing of gesture information, I was lazy to deal with it:

    In fact, it is not necessary to implement the two cases mentioned above separately. In the case of full-screen Item equal width, the maximum distance of gesture operation will not exceed the width of the Item. Therefore, you can increase the width of the key position by another Item, or the width of the screen. Check whether the value of AnimationCroller is less than or equal to the target value. If yes, terminate the animation and pass in the target value.

    First change the AnimationController created in the constructor:

    Then modify the tick method to terminate the animation return when less than;

At the end

Now look at the effect, there seems to be no big problem:

Although this is now only marginally possible by implementing a new ScrollActivity; But think about what to do with the Drag event; Or, in other words, the gesture is actually starting the DragActivity and the custom Activity together;

The UI doesn’t seem to conflict, but at the code level, there’s always conflict… This one needs to be solved