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

9 次查看(过去 30 天)
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

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by