How to move an animation with a slider in app designer?

11 views (last 30 days)
I made a simple animation of an analog clock in app designer with a while loop and a timer. I have pause and resume buttons and a slider that indicates the simulation time. So far everything works fine but I want to give control to the slider too, just as in a video, where you can drag the slider indicator forwards or backward to the exact time one wants to see.
I would really appreciate any idea. Thank you!
properties (Access = public)
PauseTime = 0; % stores total time paused
PauseHandle = tic; % stores the handle to the tic to track the time
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
close all
set(app.StartButton,'Text','Restart')
cla(app.UIAxes);
time = 60;
R = 65;
ang=linspace(0,2*pi,50);
xp=R*cos(ang);
yp=R*sin(ang);
% Animation
% Draw outter circle
patch(app.UIAxes,xp,yp,'w')
hold(app.UIAxes,'on');
axis(app.UIAxes, 'equal');
f = 0.016;
% Clock hand
rho1 = 55;
line = plot(app.UIAxes,NaN,NaN,'-*', 'LineWidth', 5, 'MarkerSize', 5, 'color', 'r');
t_h=tic;
app.PauseTime=0;
while (toc(t_h)-app.PauseTime) < time
% Clock hand
t1=toc(t_h)-app.PauseTime;
f1 = f*360;
line.XData = [0 rho1*cosd(f1*t1)] ;
line.YData = [0 rho1*sind(f1*t1)] ;
% Slider
t2=toc(t_h)-app.PauseTime;
app.Slider.Value = t2;
drawnow
end
end
% Button pushed function: PauseButton
function PauseButtonPushed(app, event)
app.PauseHandle = tic;
uiwait(app.animation)
end
% Button pushed function: ResumeButton
function ResumeButtonPushed(app, event)
app.PauseTime = app.PauseTime + toc(app.PauseHandle);
uiresume(app.animation)
end
end
  1 Comment
Adam Danz
Adam Danz on 27 Jul 2021
Assign a callback function to the slider that sets the orientation of the line.

Sign in to comment.

Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!