前言

	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. Pro ASP.NET MVC 5 Framework.学习笔记.6.4.MVC的必备工具

    2.5.创建链式依赖 当你请求Ninject创建一个类型,它检查该类型的依赖是否声明.它也会检查该依赖是否依赖其他类型.如果这里有附加依赖,Ninject自动解决他们,并创建请求的所有类的实例.正是由 ...

  2. [Unity3D][Vuforia][ios]使用vuforia的unity3d库在ios中摄像头只显示黑色,不显示摄像头,NO CAMERA的解决方案

    注:我使用的是Vuforia 4.0SDK Unity3D5.0版本,跑的ios系统为8.1 我在Vuforia官方讨论贴子中看到这其实是新手都会遇到的问题 贴子地址: https://develop ...

  3. python: linux下安装redis

    Python连接时报拒绝连接,需要重装redis: 1) 卸载redis sudo apt-get remove redis-server sudo apt-get autoremove 2)编译安装 ...

  4. 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏

    Toy Cars time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. 遍历寻找json中的重复数据

    string str = "[{\"ID\":1,\"Data\":{\"subjectCode\":\"1\" ...

  6. Python学习笔记-Day3-python内置函数

    python内置函数 1.abs    求绝对值 2.all 判断迭代器中的所有数据是否都为true 如果可迭代的数据的所有数据都为true或可迭代的数据为空,返回True.否则返回False 3.a ...

  7. 使用Jvisualvm监控JVM的内存、CPU、线程

    最近做性能测试发现很多性能问题,面对一些开发小白的数据结构思想,真想喊一声:放开那个代码,让我来!冲动. 面对WEB站点开发,性能测试是经常要做的,下面一种介绍如何结合性能测试工具,更好的监控WEB服 ...

  8. 2016年11月1日 星期二 --出埃及记 Exodus 19:17

    2016年11月1日 星期二 --出埃及记 Exodus 19:17 Then Moses led the people out of the camp to meet with God, and t ...

  9. Python 2.7.9 Demo - 三元表达式

    #coding=utf-8 #!/usr/bin/python import logging; a = 'abc'; print 'Y' if isinstance(a, str) else 'N';

  10. Wpf再次学习,分享给入门的朋友

    一.WPF介绍 先说下WPF,她的简称是Windows Presentation Foundation,注意到Presentation这个单词了吧,展现的意思,后面那个是基础,展现基础,WPF是一种展 ...