iOS - UIControl
前言
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIControl : UIView
@available(iOS 2.0, *) public class UIControl : UIView
- UIControl 从字面翻译成为控制器,可以触发事件,达到和用户进行交互。
1、UIControl 的创建
Objective-C
// 实例化 UIControl 对象
UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - 100, 100, 200, 100)]; control.backgroundColor = [UIColor redColor]; [self.view addSubview:control]; // 添加/删除触发事件
/*
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents; (id)target: 响应对象,就是触发了 control 的某个事件后响应谁的方法
(SEL)action: 触发某个事件后响应 target 的哪个方法
(UIControlEvents)controlEvents: 触发 control 的哪个事件 UIControlEventTouchDown // 按下
UIControlEventTouchDownRepeat // 双击,连续按下2次
UIControlEventTouchDragInside // 内部拖动,不松手就触发事件,一直触发
UIControlEventTouchDragOutside // 向外拖出,不松手就触发事件,一直触发
UIControlEventTouchDragEnter
UIControlEventTouchDragExit // 向外拖出,不松手就触发事件,只触发一次
UIControlEventTouchUpInside // 单击
UIControlEventTouchUpOutside // 向外拖出,松手后触发事件
UIControlEventTouchCancel UIControlEventValueChanged // 数值改变,sliders, etc. UIControlEventEditingDidBegin // 开始编辑,UITextField
UIControlEventEditingChanged // 编辑中
UIControlEventEditingDidEnd // 结束编辑
UIControlEventEditingDidEndOnExit // 结束编辑,'return key' ending editing UIControlEventAllTouchEvents // 所有事件,for touch events
UIControlEventAllEditingEvents // 所有编辑事件,for UITextField
UIControlEventApplicationReserved // range available for application use
UIControlEventSystemReserved // range reserved for internal framework use
UIControlEventAllEvents
*/ // 添加触发事件
[control addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside]; // 一个控件可以添加多个事件
[control addTarget:self action:@selector(controlClickOther:) forControlEvents:UIControlEventTouchDragOutside]; // 删除添加的事件
[control removeTarget:self action:@selector(controlClickOther:) forControlEvents:UIControlEventTouchDragOutside];
Swift
// 实例化 UIControl 对象
let control:UIControl = UIControl(frame: CGRectMake(self.view.bounds.size.width/2 - 100, 100, 200, 100)) control.backgroundColor = UIColor.redColor() self.view.addSubview(control) // 添加/删除触发事件
/*
public func addTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)
public func removeTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents) target: 响应对象,就是触发了 control 的某个事件后响应谁的方法
action: 触发某个事件后响应 target 的哪个方法
controlEvents: 触发 control 的哪个事件 TouchDown // 按下
TouchDownRepeat // 双击,连续按下2次
TouchDragInside // 内部拖动,不松手就触发事件,一直触发
TouchDragOutside // 向外拖出,不松手就触发事件,一直触发
TouchDragEnter
TouchDragExit // 向外拖出,不松手就触发事件,只触发一次
TouchUpInside // 单击
TouchUpOutside // 向外拖出,松手后触发事件
TouchCancel ValueChanged // 数值改变,sliders, etc. EditingDidBegin // 开始编辑,UITextField
EditingChanged // 编辑中
EditingDidEnd // 结束编辑
EditingDidEndOnExit // 结束编辑,'return key' ending editing AllTouchEvents // 所有事件,for touch events
AllEditingEvents // 所有编辑事件,for UITextField
ApplicationReserved // range available for application use
SystemReserved // range reserved for internal framework use
AllEvents
*/ // 添加触发事件
control.addTarget(self, action: #selector(UiControl.controlClick(_:)), forControlEvents: .TouchUpInside) // 一个控件可以添加多个事件
control.addTarget(self, action: #selector(UiControl.controlClickOther(_:)), forControlEvents: .TouchDragOutside) // 删除添加的事件
control.removeTarget(self, action: #selector(UiControl.controlClickOther(_:)), forControlEvents: .TouchDragOutside)
2、自定义点击触发事件处理
Objective-C
// 控件触发事件处理,一般响应方法都会有一个参数,没有也可以,该参数一般是触发的对象
- (void)controlClick: (UIControl *)control { } - (void)controlClickOther: (UIControl *)control { }
Swift
// 控件触发事件处理,一般响应方法都会有一个参数,没有也可以,该参数一般是触发的对象
func controlClick(control:UIControl) { } func controlClickOther(control:UIControl) { }
iOS - UIControl的更多相关文章
- iOS UIControl 详解
UIControl是UIView的子类,当然也是UIResponder的子类.UIControl是诸如UIButton,UISwitch,UItextField等控件的父类,它本身包含了一些属性和方法 ...
- iOS UIControl 事件的说明(转)
在控件事件中,简单解释下下面几个事件. 说明:由于是在“iOS 模拟器”中测试的,所以不能用手指,只能用鼠标. 1)UIControlEventTouchDown 指鼠标左键按下(注:只是“按下”)的 ...
- iOS学习24之UIControl及其子类
1. UIControl初识 1> 概述 UIControl是有控制功能的视图( 如UIButton.UISlider.UISegmentedControl等)的父类 只要跟控制有关的控件都是继 ...
- iOS学习之UIControl
一.UIControl初识 1.UIControl是有控制功能的视图(比如UIButton.UISlider.UISegmentedControl等)的父类. 只要跟控制有关的控件都是继承于 ...
- ios学习笔记之UIControl解读
UIControl,相信大家对其并不陌生吧,比如平常最常用的UIButton就是继承自UIControl的.按照惯例,还是先来看看为什么有UIControl这个类?什么时候用到它? 查下文档就可以看到 ...
- 含有按钮的ScrollView在iOS8中无法滚动的解决办法 | ScrollView with UIControl/UIButton subviews not scrollable under iOS 8
转自:http://zcw.me/blogwp/%E5%90%AB%E6%9C%89%E6%8C%89%E9%92%AE%E7%9A%84scrollview%E5%9C%A8ios8%E4%B8%A ...
- IOS开发UI基础UIControl事件
UIControl事件1.UIControlEventTouchDown单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候. 2.UIControlEventTouchDownRepeat多点触 ...
- iOS开发~视图(UIView)与控件(UIControl)
1.UIView类 1.什么是视图 看得见的都是视图 2.什么是控件 一种特殊的视图,都是UIControl的子类,不仅具有一定的显示外观,还能响应高级事件,与用户交互.严格意义上UILabel不是控 ...
- UIControl IOS控件编程 及UITextField的讲解
第一部分 UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedControl分段控件.UISlider滑块.UITextField文本字段控件.UIPageCo ...
随机推荐
- WinCE Show App Icon
找个图片,例如背景透明的jpg或png,上http://www.pic2icon.com/smartphone_wince_icon_generator.php这个网站转换下. 在项目属性中,设为图标 ...
- ORACLE 日期函数
ORACLE 日期函数 SYSDATE 当前的数据库系统时间 ADD_MONTHS(加减指定的月份) MONTHS_BETWEEN(取两个日期之间相隔的月数) LAST_DAY(取指定日期所在月的最 ...
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
Apple官方的文档为生成一个UIImage对象提供了两种方法: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路径. ...
- Docker Centos安装Redis以及问题处理
之前一篇文章 Redis安装及主从配置 介绍了redis的安装配置,另一篇文件介绍了 Docker Centos安装Openssh .今天将两篇文件结合一下——在Docker Centos环境下搭建r ...
- Python包管理工具介绍
常见的包管理工具及关系 setuptools -->distribute easy_install-->pip 1.distribute distribute是对标准库disutils模块 ...
- Python 字典(Dictionary)
字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = ...
- 在javaEE下学习web(在eclipse中开发动态的WEB工程,servlet的环境搭建,及servlet的一些方法)
一个简便的方法实现javaee版的eclipse开发动态的WEB工程(javaWEB项目)1.把开发选项切换到javaEE2. 可以在window->shou view 中找到package e ...
- Behavior Designer中的内置消息机制
最近在用Behavior Designer,其中需要用到消息机制,看了一下其中自带了这套东西 注册 Owner.RegisterEvent<string>("Message&qu ...
- 转载-python学习笔记之输入输出功能读取和写入数据
读取.写入和 Python 在 “探索 Python” 系列以前的文章中,学习了基本的 Python 数据类型和一些容器数据类型,例如tuple.string 和 list.其他文章讨论了 Pytho ...
- selenium + python 添加等待时间
转载于:http://www.blogjava.net/qileilove/articles/412450.html 四.添加等待时间 有时候为了保证脚本运行的稳定性,需要脚本中添加等待时间. 4.1 ...