drag
类: matlab.uitest.TestCase
命名空间: matlab.uitest
对 UI 组件执行拖动手势
说明
输入参数
testCase
— 测试用例
matlab.uitest.TestCase
对象
测试用例,指定为 matlab.uitest.TestCase
对象。
start
— 起始值
数值标量 | 1×2 或 1×3 数值数组
拖动手势的起始值,指定为数值标量或者 1×2 或 1×3 数值数组。start
的形式取决于 UI 组件:
旋钮和滑块 - 组件范围内的数值标量。范围由组件的
Limits
属性定义。坐标区和 UI 坐标区 - 1×2 或 1×3 数值数组,其中包含 x、y 以及可选的 z 坐标。
UI 图窗 - 1×2 数值数组,其中包含 x 和 y 坐标。指定点的坐标,以组件左下角为起点测量(以像素为单位)。
示例: 20
(旋钮)
示例: [2.5 3 1.25]
(UI 坐标区)
示例: [100 200]
(UI 图窗)
stop
— 终止值
数值标量 | 1×2 或 1×3 数值数组
拖动手势的终止值,指定为数值标量或者 1×2 或 1×3 数值数组。stop
的形式取决于 UI 组件:
旋钮和滑块 - 组件范围内的数值标量。范围由组件的
Limits
属性定义。坐标区和 UI 坐标区 - 1×2 或 1×3 数值数组,其中包含 x、y 以及可选的 z 坐标。
UI 图窗 - 1×2 数值数组,其中包含 x 和 y 坐标。指定点的坐标,以组件左下角为起点测量(以像素为单位)。
示例: 30
(旋钮)
示例: [5 3 2.25]
(UI 坐标区)
示例: [200 300]
(UI 图窗)
type
— 鼠标选择类型
'normal'
(默认) | 'extend'
| 'alt'
鼠标选择类型,指定为 'normal'
、'extend'
或 'alt'
。此输入提供关于在组件中如何按下鼠标按键的信息。有关详细信息,请参阅 UI Figure 属性。
下表列出了可能的选择类型值以及这些值对应的动作。
值 | 对应的操作 |
---|---|
| 点击鼠标左键。 |
| Shift-点击鼠标左键。 |
| 点击鼠标右键。 |
数据类型: char
| string
示例
在两个值之间拖动旋钮
创建一个旋钮。
knob = uiknob;
创建一个交互式测试用例并在两个值之间拖动旋钮。当旋钮到达终止值时,表示编程拖动手势的蓝点出现,然后消失。
testCase = matlab.uitest.TestCase.forInteractiveUse; testCase.drag(knob,13,42)
拖动滑块并验证终止值
创建一个最小值为 -237、最大值为 237、起始值为 7 的滑块。
slider = uislider("Limits",[-237 237],"Value",7);
创建一个交互式测试用例并验证滑块的初始值。
testCase = matlab.uitest.TestCase.forInteractiveUse; testCase.verifyEqual(slider.Value,7)
Verification passed.
在两个值之间拖动滑块并验证最终值。由于框架模仿用户将组件移动到任意精确值的操作,因此最好使用容差来比较实际滑块值和预期滑块值。
val = 26.75;
testCase.drag(slider,-val,val)
testCase.verifyEqual(slider.Value,val,"AbsTol",0.1)
Verification passed.
按指定选择类型在图窗上拖动
在背景颜色根据鼠标选择类型而变化的图窗上测试拖动手势。
创建一个 UI 图窗,当使用非默认鼠标选择类型在该图窗上拖动时,其背景颜色会发生变化。要对图窗行为进行编程,请通过指定其 WindowButtonMotionFcn 回调属性为图窗创建一个窗口内按键移动回调。请参阅此示例末尾的回调函数 changeColor
的代码,该函数用于根据选择类型更改背景颜色。
fig = uifigure(Color="white", ... WindowButtonMotionFcn=@(src,~)changeColor(src));
创建一个交互式测试用例,并使用它来验证图窗背景颜色是白色,即 [1 1 1]
。
testCase = matlab.uitest.TestCase.forInteractiveUse; testCase.verifyEqual(fig.Color,[1 1 1])
Verification passed.
测试用鼠标右键点击在图窗上从点 (100, 200) 拖到点 (200, 300) 的拖动手势。该手势将图窗的 SelectionType
属性设置为 'alt'
。该手势还执行回调,该回调基于 SelectionType
属性值设置图窗背景颜色。
testCase.drag(fig,[100 200],[200 300],"SelectionType","alt")
测试背景颜色现在是否为绿色,即 [0 1 0]
。测试通过。
testCase.verifyEqual(fig.Color,[0 1 0])
Verification passed.
回调函数
以下代码显示此示例中使用的回调函数。该函数查询图窗的 SelectionType
属性以识别鼠标选择类型。
function changeColor(fig) switch fig.SelectionType case 'extend' % using shift-click to drag fig.Color = "red"; case 'alt' % using right-click to drag fig.Color = "green"; end end
版本历史记录
在 R2018a 中推出MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)