前言

	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的更多相关文章

  1. iOS UIControl 详解

    UIControl是UIView的子类,当然也是UIResponder的子类.UIControl是诸如UIButton,UISwitch,UItextField等控件的父类,它本身包含了一些属性和方法 ...

  2. iOS UIControl 事件的说明(转)

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

  3. iOS学习24之UIControl及其子类

    1. UIControl初识 1> 概述 UIControl是有控制功能的视图( 如UIButton.UISlider.UISegmentedControl等)的父类 只要跟控制有关的控件都是继 ...

  4. iOS学习之UIControl

    一.UIControl初识      1.UIControl是有控制功能的视图(比如UIButton.UISlider.UISegmentedControl等)的父类. 只要跟控制有关的控件都是继承于 ...

  5. ios学习笔记之UIControl解读

    UIControl,相信大家对其并不陌生吧,比如平常最常用的UIButton就是继承自UIControl的.按照惯例,还是先来看看为什么有UIControl这个类?什么时候用到它? 查下文档就可以看到 ...

  6. 含有按钮的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 ...

  7. IOS开发UI基础UIControl事件

    UIControl事件1.UIControlEventTouchDown单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候. 2.UIControlEventTouchDownRepeat多点触 ...

  8. iOS开发~视图(UIView)与控件(UIControl)

    1.UIView类 1.什么是视图 看得见的都是视图 2.什么是控件 一种特殊的视图,都是UIControl的子类,不仅具有一定的显示外观,还能响应高级事件,与用户交互.严格意义上UILabel不是控 ...

  9. UIControl IOS控件编程 及UITextField的讲解

    第一部分 UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedControl分段控件.UISlider滑块.UITextField文本字段控件.UIPageCo ...

随机推荐

  1. git 上传本地文件到github

    git 上传本地文件到github 1 git config --global user.name "Your Real Name" 2 git config --global u ...

  2. 怎样新建Oracle数据库

    新建Oracle数据库三种方法:1.通过运行Oracle Database Configuration Assistant 创建配置或删除数据库(也可在命令行下输入dbca):2.用命令行的方式建立数 ...

  3. oracle nvl和nvl2的区别

    一直用oracle nvl函数,最近发现还有一个nvl2函数: nvl(a,b) 如果a不为null 则返回a,如果a为null则返回b; nvl2(a,b,c) ,如果a不为null 则返回b,如果 ...

  4. ACM题目————还是畅通工程

    Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...

  5. android异步加载图片并缓存到本地实现方法

    图片过多造成内存溢出,这个是最不容易解决的,要想一些好的缓存策略,比如大图片使用LRU缓存策略或懒加载缓存策略.今天首先介绍一下本地缓存图片     在android项目中访问网络图片是非常普遍性的事 ...

  6. python中给for循环增加索引

    for index, item in enumerate(Foo()): print "index: ", index, " item: ",item 用enu ...

  7. Python3发送post请求,自动记住cookie

    转载自:http://www.cnblogs.com/meitian/p/4607737.html 在做登录的post请求时,需要记住cookie,否则不能访问登录后的页面. 下面是登录的代码: #c ...

  8. eclipse 下调整jdk和tomcat的jvm参数

    eclipse 下调试和运行,往往会出现调整java.lang.OutOfMemoryError: Java heap space  产生的原因我猜测是使用了maven,subversion,myla ...

  9. 取模(mod)

    取模(mod) [题目描述] 有一个整数a和n个整数b_1, …, b_n.在这些数中选出若干个数并重新排列,得到c_1,…, c_r.我们想保证a mod c_1 mod c_2 mod … mod ...

  10. c#复习阶段

    在控制台程序中使用结构体.集合,完成下列要求项目要求:一.连续输入5个学生的信息,每个学生都有以下4个内容:1.序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1,第二个是2 ...