2010-07-04 33 views
1

我试图在轴上绘制一条线,点击时只要按下鼠标按钮,当按钮释放时,线将停止跟随。简而言之,根据点击和拖动将图形上的一条线重新定位到新的位置。WindowButtonUpFcn问题:它为什么不释放?

我已经能够开始让线跟随鼠标指针,问题是得到WindowButtonUpFcn停止跟随鼠标线。即如何关闭WindowButtonMotionFcn?

这是代码。这很粗糙,因为它只是一个小型测试程序,所以不要批评太多。

function pushbutton1_Callback(hObject, eventdata, handles) 
% hObject handle to pushbutton1 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 
    x = 0:.1:10; 
    y = zeros(size(x))+.5; 
    line(x,y, 'Tag', 'newLine'); 
    set(findobj('Tag', 'newLine'),'ButtonDownFcn',@button_down) 
end 

function button_down(src,evnt) 
% src - the object that is the source of the event 
% evnt - empty for this property 
set(src,'Selected','on') 
set(gcf, 'WindowButtonMotionFcn', @button_motion); 
end 

function button_motion(src, evnt) 
h = findobj('Tag', 'axes1'); 
pos = get(h, 'CurrentPoint'); 
disp(pos); 
hndl = findobj('Tag', 'newLine'); 
    delete(hndl); 
    x = 0:.1:10; 
    y = zeros(size(x))+pos(3);  
    line(x,y, 'Tag', 'newLine'); 
    set(gcf, 'WindowButtonUpFcn', @button_up); 
end 

function button_up(src, evnt) 
    %What to do here?  
end 

回答

2

这里有几个技巧:

  • 除了删除和型重构线在你button_motion功能,您应该使用SET命令来修改线对象的'XData''YData'性质该行的新位置。这将使更平滑的动画。

  • 你应该将这一行:从button_motion

    set(gcf, 'WindowButtonUpFcn', @button_up); 
    

    button_down

  • 在你button_motion功能,你应该在drawnow调用添加到年底给力剧情即时更新最重要的是给button_up功能的地方,它可以中断button_motion

  • 在你button_up功能,只需设置身影[]而行,以'off''Selected'财产'WindowButtonMotionFcn''WindowButtonUpFcn'性能。

+0

制定出很好,谢谢你gnovice。 – Michael 2010-07-04 03:23:47

+0

+1为完整的解决方案,并弥补缺乏接受。 – Jonas 2010-07-04 19:05:20