How to move an animation with a slider in app designer?
조회 수: 11 (최근 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
댓글 수: 1
Adam Danz
2021년 7월 27일
Assign a callback function to the slider that sets the orientation of the line.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!