Frames and time
Move a dot from one end of a track to the other in 600 milliseconds. Tap while it is moving to reverse it from its current position.
The animation uses elapsed time, not a fixed number of frames. A display that produces fewer callbacks should still reach the destination after the same duration.
using System;
using Xui.Core.Animation;
using Xui.Core.Canvas;
using Xui.Core.DI;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;
public sealed class TransitionView : View
{
private NFloat position = (NFloat)0.15;
private NFloat from;
private NFloat target = (NFloat)0.15;
private TimeSpan? start;
private bool running;
private IDeviceInfo? device;
protected override void OnActivate()
{
device = this.GetService<IDeviceInfo>();
if (device is not null)
device.Changed += OnPreferencesChanged;
}
protected override void OnDeactivate()
{
if (device is not null)
device.Changed -= OnPreferencesChanged;
device = null;
running = false;
position = target;
start = null;
InvalidateRender();
}
private void OnPreferencesChanged()
{
if (device?.PrefersReducedMotion == true)
{
running = false;
position = target;
InvalidateRender();
}
}
public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
{
if (phase != EventPhase.Bubble || e.Type != PointerEventType.Down || e.State.Button != PointerButton.Left)
return;
from = position;
target = target < (NFloat)0.5 ? (NFloat)0.85 : (NFloat)0.15;
start = null;
running = device?.PrefersReducedMotion != true;
if (running)
RequestAnimationFrame();
else
position = target;
InvalidateRender();
}
protected override void AnimateCore(TimeSpan previousTime, TimeSpan currentTime)
{
if (!running)
return;
start ??= currentTime;
NFloat t = NFloat.Clamp((NFloat)((currentTime - start.Value).TotalSeconds / 0.6), 0, 1);
position = from + (target - from) * Easing.EaseInOutCubic(t);
InvalidateRender();
if (t < 1)
RequestAnimationFrame();
else
{
position = target;
running = false;
}
}
protected override void RenderCore(IContext context)
{
context.SetFill(new Color(0xFAF7FFFF));
context.FillRect(Frame);
NFloat travel = NFloat.Max(0, Frame.Width - 64);
context.SetFill(new Color(0xEADCF5FF));
context.FillRect(new Rect(Frame.X + 32, Frame.Center.Y - 2, travel, 4));
context.BeginPath();
context.Arc((Frame.X + 32 + travel * position, Frame.Center.Y), 24, 0, NFloat.Pi * 2);
context.SetFill(new Color(0x8538CEFF));
context.Fill();
context.SetFont(new Font(18, "Inter"));
context.TextAlign = TextAlign.Center;
context.FillText("Tap to move; tap again to reverse", (Frame.Center.X, Frame.Y + 56));
}
}
Request work only while something moves
A press chooses the destination and calls RequestAnimationFrame. Xui later calls AnimateCore(previousTime, currentTime). That method advances the state, invalidates the picture, and requests another frame only if the transition is still running.
RequestAnimationFrame and InvalidateRender do different jobs. The first asks for another time update; the second says the picture has changed. Request both while advancing a visual animation, then stop requesting time updates at rest.
The view has no animated children. If you add children and override AnimateCore, call base.AnimateCore(previousTime, currentTime) so their animation continues too.
Normalize elapsed time
The first callback records currentTime as the start. Each later callback computes elapsed seconds divided by 0.6, then clamps that fraction to zero through one. The easing function converts the fraction to a smooth progression, and interpolation maps it between the stored start and target positions.
Do not add a fixed amount such as 0.016 on every frame. Frame intervals change with refresh rate, load, and app visibility. For velocity-based simulation, use the actual delta between the supplied times and choose an explicit policy for a long pause; for this finite transition, the elapsed fraction simply reaches its endpoint.
Avoid mixing DateTime.Now with the frame clock. The supplied times let deterministic tests advance the same animation without relying on wall-clock timing.
Interrupt without a visual jump
On a second press, save the currently displayed position as the new start, reverse the target, and reset the start timestamp. The next frame begins from that position. Restarting from the old endpoint would make the dot jump before moving back.
This preserves position, not velocity. A new easing curve can change the instantaneous speed. Use a motion model that carries velocity when continuous momentum matters; a simple reversible UI transition usually needs only a stable position.
At the last frame, store the exact target and stop. Try repeated presses, a slow machine, and navigating away mid-animation. The lifetime guide explains the cleanup and reduced-motion behavior included in the example.