UIcontrol中touch事件uicontrolevents的一点东东

本文详细解析UIKit中的各种事件,包括UIControlEventTouchDown等,并通过实际代码验证这些事件的触发条件。

由于在学习时有个作业需要稍微用到这些东西,本人找了点东西验证下我自己的理解  。

http://blog.youkuaiyun.com/heng615975867/article/details/39321081#comments

这个是我找到的博客 。算是验证了我自己的猜测。


以下为博主原文

http://blog.163.com/cz_jdton/blog/static/92732504201282543017312/
在这个网友的日志里看到一些内容,大体如下:
UIControlEventTouchDown
单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
当一次触摸从控件窗口内部拖动到外部时。
 
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents

通知所有事件。


这里可以发现他对events的解释很系统虽然有些东西还是讲不通。


但是有几个重要点说明白了

UIControlEventTouchDown和UIControlEventTouchUpInside

如果我们点击一次我们的UIcontrol控件。能触发的是两个事件,touchdown和touchUpInside都能触发

所以这里touchUpInside并不是单机触发。而是在控件里抬起手指才触发。

同理touchUpOutside也并不是在控件外单机触发,而是在控件外抬起手指时触发。



该博主为了验证以上的解释做了一系列的验证措施。

措施如下:



001 //
002 //  ViewController.m
003 //  UIControlEvents
004 //
005 //  Created by WeiZhen_Liu on 13-4-27.
006 //  Copyright (c) 2013年 WeiZhen_Liu. All rights reserved.
007 //
008  
009 #import "ViewController.h"
010  
011 @interface ViewController ()
012  
013 @end
014  
015 @implementation ViewController
016  
017 - (void)viewDidLoad
018 {
019     [super viewDidLoad];
020     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
021     [button setFrame:CGRectMake(10, 100, 300, 40)];
022     [button setTitle:@"Test" forState:UIControlStateNormal];
023     [button addTarget:self action:@selector(TouchDown:) forControlEvents:UIControlEventTouchDown];
024     [button addTarget:self action:@selector(TouchDownRepeat:) forControlEvents:UIControlEventTouchDownRepeat];
025     [button addTarget:self action:@selector(TouchDragInside:) forControlEvents:UIControlEventTouchDragInside];
026     [button addTarget:self action:@selector(TouchDragOutside:) forControlEvents:UIControlEventTouchDragOutside];
027     [button addTarget:self action:@selector(TouchDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
028     [button addTarget:self action:@selector(TouchDragExit:) forControlEvents:UIControlEventTouchDragExit];
029     [button addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
030     [button addTarget:self action:@selector(TouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
031     [button addTarget:self action:@selector(TouchCancel:) forControlEvents:UIControlEventTouchCancel];
032     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPress:)];
033     longPress.minimumPressDuration = 3;
034     [button addGestureRecognizer:longPress];
035     [longPress release];
036     [self.view addSubview:button];
037 }
038  
039 - (void)TouchDown:(id)sender
040 {
041     NSLog(@"%@", NSStringFromSelector(_cmd));
042 }
043  
044  
045 - (void)TouchDownRepeat:(id)sender
046 {
047     NSLog(@"%@", NSStringFromSelector(_cmd));
048 }
049  
050 - (void)TouchDragInside:(id)sender
051 {
052     NSLog(@"%@", NSStringFromSelector(_cmd));
053 }
054  
055 - (void)TouchDragOutside:(id)sender
056 {
057     NSLog(@"%@", NSStringFromSelector(_cmd));
058 }
059  
060 - (void)TouchDragEnter:(id)sender
061 {
062     NSLog(@"%@", NSStringFromSelector(_cmd));
063 }
064  
065 - (void)TouchDragExit:(id)sender
066 {
067     NSLog(@"%@", NSStringFromSelector(_cmd));
068 }
069  
070 - (void)TouchUpInside:(id)sender
071 {
072     NSLog(@"%@", NSStringFromSelector(_cmd));
073 }
074  
075 - (void)TouchUpOutside:(id)sender
076 {
077     NSLog(@"%@", NSStringFromSelector(_cmd));
078 }
079  
080 - (void)TouchCancel:(id)sender
081 {
082     NSLog(@"%@", NSStringFromSelector(_cmd));
083 }
084  
085 - (void)didReceiveMemoryWarning
086 {
087     [super didReceiveMemoryWarning];
088     // Dispose of any resources that can be recreated.
089 }
090  
091 - (void)buttonLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
092 {
093     if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
094         NSLog(@"%@", NSStringFromSelector(_cmd));
095     }
096 }
097  
098 @end
099  
100 /**
101  * UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
102  * UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
103  * UIControlEventTouchDragInside     = 1 <<  2,
104  * UIControlEventTouchDragOutside    = 1 <<  3,
105  * UIControlEventTouchDragEnter      = 1 <<  4,   // 注:我在模拟器上找了好久没找到这个东西什么时候触法
106  * UIControlEventTouchDragExit       = 1 <<  5,
107  * UIControlEventTouchUpInside       = 1 <<  6,
108  * UIControlEventTouchUpOutside      = 1 <<  7,
109  * UIControlEventTouchCancel         = 1 <<  8,  
110  * 按下松开,依次触发:   TouchDown:      TouchUpInside:
111  * 按下并在按钮内部拖动最后松开,依次触发:  TouchDown:    TouchDragInside:(多次)    TouchUpInside:
112  * 按着按钮拖动到按钮外不远处,依次触发: TouchDown:    TouchDragInside:(多次)    TouchUpInside:
113  * 按着按钮拖动到按钮外很远处,依次触发: TouchDown:    TouchDragInside:(多次)    TouchDragExit:    TouchDragOutside:(多次)    TouchUpOutside:
114  * 快速双击两下,依次触发: TouchDown:    TouchUpInside:    TouchDown:    TouchDownRepeat:    TouchUpInside:
115  * 快速击三下,依次触发: TouchDown:    TouchUpInside:    TouchDown:    TouchDownRepeat:    TouchUpInside:    TouchDown:    TouchDownRepeat:    TouchUpInside:
116  * 长按会依次触发: TouchDown:    buttonLongPress:    TouchCancel:
117  */
118 ------------------------------------------------------------------------------------------------------------

这里很明显能看出来 按下松开(单机)是依次触发touchdown 和touchUpInside!!!!!依次触发!!!!!依次触发!!!!!重要的事情说三遍!

touchdown只要你的手指触摸到了控件空间 就会触发的事件。

同样这里是在控件内按下拖出控件空间再松开触发的是touchdown draginside dragexit  dragoutside 然后是touchupoutside 所以说TouchUpOutside是在控件空间外松开手指才触发的事件。

对于controlEvents的误解还是因为英文不好啊。touchUp明显就是说明手指抬起的动作。

同时看到另一个博客,博主对于各种触发条件的解释更为详细

http://www.cnblogs.com/whyandinside/archive/2013/10/26/3388908.html

以下为博主原文:

在控件事件中,简单解释下下面几个事件。
说明:由于是在“iOS 模拟器”中测试的,所以不能用手指,只能用鼠标。
1)UIControlEventTouchDown
指鼠标左键按下(注:只是“按下”)的动作


2)UIControlEventTouchDownRepeat
指鼠标左键连续多次重复按下(注:只是“按下”)的动作,比如,鼠标连续双击、三击、……、多次连击。
说明:多次重复按下时,事件序列是这样的:
UIControlEventTouchDown -> (UIControlEventTouchUpInside) -> UIControlEventTouchDown -> UIControlEventTouchDownRepeat -> (UIControlEventTouchUpInside) -> UIControlEventTouchDown -> UIControlEventTouchDownRepeat -> (UIControlEventTouchUpInside) ->...
除了第一次按下外,后面每次摁下都是一个UIControlEventTouchDown事件,然后紧跟一个UIControlEventTouchDownRepeat事件。


3)UIControlEventTouchDragInside
指按下鼠标,然后在控件边界范围内拖动。


4)UIControlEventTouchDragOutside
与UIControlEventTouchDragInside不同的是,拖动时,鼠标位于控件边界范围之外。但首先得有个UIControlEventTouchDown事件,然后接一个UIControlEventTouchDragInside事件,再接一个UIControlEventTouchDragExit事件,这时,鼠标已经位于控件外了,继续拖动就是UIControlEventTouchDragOutside事件了。
具体操作是:在控件里面按下鼠标,然后拖动到控件之外。


5)UIControlEventTouchDragEnter
指拖动动作中,从控件边界外到内时产生的事件。


6)UIControlEventTouchDragExit
指拖动动作中,从控件边界内到外时产生的事件。


7)UIControlEventTouchUpInside
指鼠标在控件范围内抬起,前提先得按下,即UIControlEventTouchDown或UIControlEventTouchDownRepeat事件。


8)UIControlEventTouchUpOutside
指鼠标在控件边界范围外抬起,前提先得按下,然后拖动到控件外,即 UIControlEventTouchDown -> UIControlEventTouchDragInside(n 个) -> UIControlEventTouchDragExit -> UIControlEventTouchDragOutside(n 个) 时间序列,再然后就是抬起鼠标,产生UIControlEventTouchUpOutside事件。

看这个博主的解释能更好的理解各种事件的触发条件。

希望大家对ControlEvent的理解更进一步。

<think> 首先,用户的问题是关于MATLAB中uipanel和uicontrol的使用方法及相关介绍。用户没有提供具体的问题上下文,但从系统指令和引用内容来看,我需要基于提供的引用内容来构建回答。 系统级指令: - 所有行内数学表达式必须使用$...$格式。 - 独立公式必须使用$$...$$格式并单独成段。 - LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识,例如 [^1]。 - 用户可见层:回答结构清晰,帮助用户逐步解决问题,保证回答真实可靠。 引用内容: - 引用[1]: 描述了MATLAB应用开发,从uicontrol到UI组件的过渡,强调使用uifigure的优势。 - 引用[2]: 列举了uicontrol可以创建的控件类型。 - 引用[3]: 讨论了MATLAB GUI布局,特别是水平、垂直、网格布局。 - 引用[4]: 展示了如何使用滚动条和滚轮实现uipanel的动态效果。 用户需求:用户想了解uipanel和uicontrol的使用方法和相关介绍。我需要综合引用内容,提供清晰的结构化回答。 回答结构: 1. **引言**:简要介绍uipanel和uicontrol在MATLAB GUI开发中的作用。 2. **uicontrol的使用方法**: - 解释uicontrol是什么。 - 列举常见的uicontrol类型(如按钮、文本框等)。 - 展示基本示例代码。 3. **uipanel的使用方法**: - 解释uipanel是什么。 - 讨论布局工具(如水平、垂直、网格布局)。 - 展示示例代码。 4. **结合使用**:如何将uicontrol放在uipanel中。 5. **过渡到现代UI**:提及从传统figure/uicontrol过渡到uifigure/UI组件的优势(基于引用[1])。 6. **相关注意事项**:如单位设置、位置属性等。 7. **生成相关问题**:在末尾添加相关问题。 确保使用中文回答。引用内容要自然地添加引用标识,例如 [^1]。 数学表达式:由于主题是MATLAB GUI,可能没有太多数学表达式。但如果有,比如涉及计算,需用$...$格式。例如,位置属性如$[x, y, width, height]$。 生成相关问题:在回答末尾,添加3-4个相关问题,格式如示例中的"§§相关问题§§"。 完整结构: - 开头:介绍。 - 主体:分点解释。 - 代码块:如果适用,使用代码块展示示例。 - 结尾:相关问题。 参考引用: - 当提到uicontrol类型时,引用[2]。 - 当讨论布局时,引用[3]。 - 当提到uipanel动态效果时,引用[4]。 - 当讨论过渡到uifigure时,引用[1]。 示例代码:基于引用内容提供简单示例。 </think> ### MATLAB中uipanel和uicontrol的使用方法及相关介绍 在MATLAB GUI开发中,`uipanel`和`uicontrol`是创建用户界面的核心组件。`uicontrol`用于添加交互控件(如下压按钮、文本框等),而`uipanel`则作为容器来组织和布局这些控件。基于传统`figure`和`uicontrol`的方法已被现代`uifigure`和UI组件部分取代,但理解其基础仍至关重要。以下我将逐步介绍使用方法,确保内容基于引用内容并真实可靠。 #### 1. **uicontrol的使用方法** `uicontrol`用于创建各种交互控件,所有控件需放置在`figure`或`uipanel`中。语法为: ```matlab h = uicontrol('Parent', parent_handle, 'Style', style_type, ...); ``` - **常见控件类型**(基于引用[2]): - 下压按钮(`'pushbutton'`):用于触发操作,如`uicontrol('Style','pushbutton','Callback',@myFunction)`。 - 可编辑文本框(`'edit'`):接收用户输入,如`uicontrol('Style','edit','String','请输入文本')`。 - 单选按钮(`'radiobutton'`)和复选框(`'checkbox'`):用于选项选择。 - 滑动条(`'slider'`):调节数值范围,需设置`Min`、`Max`和`Value`属性。 - 静态文本标签(`'text'`):显示只读信息。 引用[2]详细列举了这些控件类型[^2]。 - **示例代码**(创建一个简单按钮): ```matlab fig = figure; % 创建主窗口 btn = uicontrol(fig, 'Style', 'pushbutton', 'String', '点击我', ... 'Position', [100 100 100 30], 'Callback', @(~,~) disp('按钮已点击')); ``` 这里,`Position`属性控制位置,格式为$[x, y, width, height]$,单位默认为像素。 #### 2. **uipanel的使用方法** `uipanel`是一个容器组件,用于分组多个`uicontrol`控件,提升界面组织性。语法为: ```matlab panel = uipanel('Parent', parent_handle, 'Title', '面板标题', ...); ``` - **关键属性**: - `Position`:设置面板位置和大小,例如$[0, 0, 400, 300]$表示从原点开始的宽高。 - `Units`:指定单位(如`'pixels'`或`'normalized'`),推荐`'pixels'`便于精确控制[^4]。 - `BorderType`:边框样式(如`'none'`隐藏边框)。 引用[4]展示了如何通过滚动条动态调整`uipanel`位置[^4]。 - **布局工具**(结合引用[3]): - **水平布局**:控件水平排列,需手动计算位置。 - **垂直布局**:控件垂直堆叠。 - **网格布局**:使用`uigridlayout`(现代UI组件)简化,但传统方法需设置`Position`。 引用[3]强调布局工具可避免繁琐的`Position`计算[^3]。 - **示例代码**(在面板中添加按钮): ```matlab fig = figure; panel = uipanel(fig, 'Title', '控制面板', 'Position', [50 50 300 200], 'Units', 'pixels'); btn_inside = uicontrol(panel, 'Style', 'pushbutton', 'String', '面板内按钮', ... 'Position', [50 50 100 30]); ``` #### 3. **结合使用uipanel和uicontrol** 将`uicontrol`放置在`uipanel`中可实现分层界面: - **步骤**: 1. 创建`figure`作为顶层窗口。 2. 添加`uipanel`作为子容器。 3. 在`uipanel`内部添加多个`uicontrol`。 - **动态效果**:如引用[4]所示,可通过滚动条改变`uipanel`的位置实现动态移动[^4]: ```matlab % 示例:滚动条控制面板位置 fig = figure; slider = uicontrol(fig, 'Style', 'slider', 'Position', [10 10 400 20], ... 'Min', -300, 'Max', 0, 'Value', -240, ... 'Callback', @(src,~) set(panel, 'Position', [0 src.Value 460 540])); panel = uipanel(fig, 'Units', 'pixels', 'Position', [0 -240 460 540], ... 'BorderType', 'none'); ``` #### 4. **过渡到现代UI组件** 虽然`uicontrol`和`uipanel`仍可用,但MATLAB推荐迁移到基于`uifigure`的UI组件(如`uibutton`、`uipanel`新版),原因包括: - **优势**:新组件支持更多控件类型(如树形视图)、响应式布局和主题定制[^1]。 - **迁移建议**: - 使用`uifigure`替代`figure`。 - 用`uibutton`等函数替换`uicontrol`。 引用[1]提供了详细迁移指南[^1]。 #### 注意事项 - **单位设置**:始终指定`Units`属性(如`'pixels'`)以避免分辨率问题。 - **回调函数**:为`uicontrol`定义`Callback`属性以处理用户交互。 - **性能**:复杂界面建议使用App Designer工具,它基于`uifigure`,能自动处理布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值