★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ )
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10869372.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

函数参数既可以具有名称(在函数体内使用),也可以具有参数标签(在调用函数时使用)。

如函数参数标签和参数名称中所述。方法参数也是如此,因为方法只是与类型相关联的函数。

  1 //
  2 //  ViewController.swift
  3 //  demo
  4 //
  5 //  Created by qiang zeng on 2020/2/20.
  6 //  Copyright © 2020 strengthen All rights reserved.
  7 //
  8
  9 import UIKit
 10
 11 class ViewController: UIViewController {
 12     override func viewDidLoad() {
 13         super.viewDidLoad()
 14         //初始化一个按钮
 15         let button:UIButton = getButton()
 16         //将按钮添加到视图中
 17         self.view.addSubview(button)
 18
 19         //设置按钮的点击事件:button.addTarget(Any?, action: Selector, for: UIControlEvents)
 20         //1、touchDown:单点触摸按下事件,点触屏幕
 21         //2、touchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
 22         //3、touchDragInside:触摸在控件内拖动时
 23         //4、touchDragOutside:触摸在控件外拖动时
 24         //5、touchDragEnter:触摸从控件之外拖动到内部时
 25         //6、touchDragExit:触摸从控件内部拖动到外部时
 26         //7、touchUpInside:在控件之内触摸并抬起事件
 27         //8、touchUpOutside:在控件之外触摸抬起事件
 28         //9、touchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
 29
 30         //按钮绑定点击事件,无参数传递方式1
 31         button.addTarget(self, action: #selector(buttonClick1), for: .touchUpInside)
 32         button.addTarget(self, action: #selector(buttonClick2), for: .touchUpInside)
 33         button.addTarget(self, action: #selector(buttonClick3), for: .touchUpInside)
 34         //按钮绑定点击事件,无参数传递方式2
 35         //Swift提倡用方式2
 36         button.addTarget(self, action: #selector(ViewController.buttonClick1), for: .touchUpInside)
 37         button.addTarget(self, action: #selector(ViewController.buttonClick2), for: .touchUpInside)
 38         button.addTarget(self, action: #selector(ViewController.buttonClick3), for: .touchUpInside)
 39
 40         //按钮绑定点击事件,带button参数传递
 41         //与点击方法的参数相同,无参数标签使用参数名称,传递点击的按钮
 42         button.addTarget(self, action: #selector(buttonClick2(button:)), for: .touchUpInside)
 43
 44         //与点击方法的参数相同,有参数标签使用参数标签
 45         button.addTarget(self, action: #selector(buttonClick3(_ :)), for: .touchUpInside)
 46     }
 47
 48     //MARK:按钮点击1
 49     @objc func buttonClick1()
 50     {
 51         //TODO
 52     }
 53
 54     //MARK:按钮点击2
 55     @objc func buttonClick2(button:UIButton)
 56     {
 57         //TODO
 58     }
 59
 60     //MARK:按钮点击3
 61     @objc func buttonClick3(_ button:UIButton)
 62     {
 63         //TODO
 64     }
 65
 66     //MARK:获取按钮
 67     func getButton() -> UIButton
 68     {
 69         //1、UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
 70         //2、UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
 71         //3、UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
 72         //4、UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
 73         //5、UIButtonType.infoDark:为感叹号“!”圆形按钮
 74         //6、UIButtonType.infoLight:为感叹号“!”圆形按钮
 75         let button:UIButton = UIButton(type:.custom)
 76
 77         //设置按钮的坐标和显示区域
 78         button.frame = CGRect(x:50, y:50, width:50,height: 50)
 79         //设置按钮的背景颜色
 80         button.backgroundColor = UIColor.red
 81         //设置按钮背景图片:button.setBackgroundImage(UIImage?, for: UIControlState)
 82         button.setBackgroundImage(UIImage(named:"bg"), for: .normal)
 83         //设置按钮字体和大小
 84         button.titleLabel?.font = UIFont.init(name:"Arial",size:16)
 85         //设置字体大小
 86         button.titleLabel?.font = UIFont.systemFont(ofSize: 22)
 87         //Tag
 88         button.tag = 1
 89         //高亮状态
 90         button.isHighlighted = true
 91         //设置镂空图片的颜色
 92         button.tintColor = UIColor.white
 93         //设置圆角
 94         button.layer.masksToBounds = true
 95         //圆角半径
 96         button.layer.cornerRadius = 10
 97         //边框粗细
 98         button.layer.borderWidth = 0.5
 99         //设置边框
100         button.layer.borderColor = UIColor.black.cgColor
101
102         //设置按钮不同状态下的文字、颜色和阴影颜色
103         //1、UIControlState.normal//普通状态
104         //2、UIControlState.highlighted//触摸状态
105         //3、UIControlState.disabled//禁用状态
106         //设置各个状态的文字内容
107         button.setTitle("普通状态", for: .normal)
108         button.setTitle("触摸状态", for: .highlighted)
109         button.setTitle("禁用状态", for: .disabled)
110         //设置各个状态下文字阴影的颜色
111         button.setTitleShadowColor(UIColor.green, for:.normal)
112         button.setTitleShadowColor(UIColor.yellow, for:.highlighted)
113         button.setTitleShadowColor(UIColor.gray, for:.disabled)
114
115         //设置按钮图标:button.setImage(UIImage?, for: UIControlState)
116         //当按钮类型为custom,处于highlighted和disable状态下图标会变暗,
117         //可以通过设置来禁用这种情况
118         button.setImage(UIImage(named:"img"), for: .normal)
119         button.adjustsImageWhenHighlighted = false //使触摸模式下按钮也不会变暗
120         button.adjustsImageWhenDisabled = false //使禁用模式下按钮也不会变暗
121
122         //按钮文字太长处理方式:titleLabel 的 lineBreakMode 属性
123         //byTruncatingHead:省略头部文字,省略部分用...代替
124         //byTruncatingMiddle:省略中间部分文字,省略部分用...代替(默认)
125         //byTruncatingTail:省略尾部文字,省略部分用...代替
126         //byClipping:直接将多余的部分截断
127         //byWordWrapping:自动换行(按词拆分)
128         //byCharWrapping:自动换行(按字符拆分)
129         //注意:当设置自动换行后(byWordWrapping 或 byCharWrapping),我们可以在设置 title 时通过 \n 进行手动换行。
130         button.titleLabel?.lineBreakMode = .byWordWrapping
131
132         //调整文字和图标之间的间距:通过图片偏移量(imageEdgeInsets)设置间距
133         button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
134         //通过文字偏移量(titleEdgeInsets)设置间距
135         button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
136         //调换或调整文字和图片位置:通过设置 titleEdgeInsets 和 imageEdgeInsets 即可实现。
137         let imageSize = button.imageRect(forContentRect: button.frame)//获取图标的CGRect
138         let titleFont = button.titleLabel?.font//获取字体
139         let titleSize = button.currentTitle!.size(withAttributes:[NSAttributedString.Key.font: titleFont!])//获取文字的尺寸
140         button.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2) , bottom: 0, right: 0)
141         button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(titleSize.width * 2 + 10))
142
143         //扩展方法示例:快速的设置图片和文字的相对位置,以及间距
144         button.set(icon: UIImage(named:"img"), title: "标题", titleLocation: .top, padding: 10, state: .normal)
145         return button
146     }
147 }
148
149 //MARK:
150 extension UIButton{
151     func set(icon image: UIImage?, title: String, titleLocation: UIView.ContentMode, padding: CGFloat, state: UIControl.State ) {
152         self.imageView?.contentMode = .center
153         self.setImage(image, for: state)
154         relativeLocation(title: title, location: titleLocation, padding: padding)
155         self.titleLabel?.contentMode = .center
156         self.setTitle(title, for: state)
157     }
158
159     private func relativeLocation(title: String, location: UIView.ContentMode, padding: CGFloat){
160         let imageSize = self.imageRect(forContentRect: self.frame)
161         let titleFont = self.titleLabel?.font!
162         let titleSize = title.size(withAttributes: [NSAttributedString.Key.font : titleFont!])
163
164         var titleInsets: UIEdgeInsets
165         var imageInsets: UIEdgeInsets
166
167         switch location {
168         case .top:
169             titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + padding),
170                                        left: -(imageSize.width), bottom: 0, right: 0)
171             imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
172         case .left:
173             titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
174             imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(titleSize.width * 2 + padding))
175         case .bottom:
176             titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + padding),
177                                        left: -(imageSize.width), bottom: 0, right: 0)
178             imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
179         case .right:
180             titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -padding)
181             imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
182         default:
183             titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
184             imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
185         }
186         self.titleEdgeInsets = titleInsets
187         self.imageEdgeInsets = imageInsets
188     }
189 }

转载于:https://www.cnblogs.com/strengthen/p/10869372.html

[Swift]最强UIButton解析 | #selector()绑定点击事件相关推荐

  1. li通过绑定点击事件实现类似购物车功能(全选,点击勾选、取消等),此例为模仿移动端小说书架界面实现勾选取消功能展示,有些麻烦但好理解(前端xb要努力变强)

    效果展示 点击全选被全部勾选,再点击则取消勾选(下方还有取消按钮,实现同样功能). 也可以实现点击的书籍被勾选,再次点击则取消勾选 在功能实现过程中主要是对v-show.v-if.v-else等的灵活 ...

  2. jquery 动态按钮绑定点击事件

    一.Jquery实现点击事件的四种写法 来自:https://blog.csdn.net/liutianjie/article/details/85112787 1. $(".tab&quo ...

  3. jquery动态渲染绑定点击事件不生效

    原因:动态添加的HTML元素是在CSS,JS代码加载完成后再渲染的HTML页面.浏览器解析这些通过ajax请求到后台返回的数据,再根据返回的结果动态生成HTML页面时,这时绑定事件的标签元素还没有生成 ...

  4. react中绑定点击事件_在React中绑定事件处理程序的最佳方法

    react中绑定点击事件 by Charlee Li 通过李李 在React中绑定事件处理程序的最佳方法 (The best way to bind event handlers in React) ...

  5. JavaScript给按钮绑定点击事件(onclick)的方法及js常见事件

    本文实例讲述了JavaScript给按钮绑定点击事件(onclick)的方法.分享给大家供大家参考.具体分析如下: 我们可以通过设定按钮的onclick属性来给按钮绑定onclick事件 <!D ...

  6. javascript动态生成按钮并绑定点击事件

    前一个多月公司的前端开发人员离职,公司一直在招前端,基本上每天都有几个来面试的人,就这个面试频率,也才招了一个多月才招到.绝大部分都是死在了一道上机面试提上.题目很基础,但也很考基本功,基本上才3%左 ...

  7. 【JavaScript】按钮绑定点击事件-onCliek事件

    [JavaScript]按钮绑定点击事件-onCliek事件 <button type="submit" id="btn">btn</butt ...

  8. jQuery动态添加按钮,绑定点击事件失效

    jQuery动态添加按钮,绑定点击事件失效 因为需求需要给页面动态添加按钮并绑定点击事件进行操作, 但是发现绑定的点击事件失效. 原因分析: append中的节点是在整个文档加载完之后开始添加,因此页 ...

  9. jquey javascript 绑定点击事件(click事件无反应,因js获取不到当前的点击项)

    问题: 如果DOM元素是动态生成的,在这种情况下为该元素绑定click时间,发现点击事件无效,设置debugger,console.log也没用 解决: juery:为目标元素的父元素,或者是整个文档 ...

最新文章

  1. 项目延期半年,我被软件外包坑惨了!
  2. java 10进制转2进制递归算法_十进制转二进制
  3. oracle修改redo路径,oracle修改redo log files路径
  4. shiro源码篇 - 疑问解答与系列总结,你值得拥有
  5. [C指针] 用图表解读C声明:Unscrambling C Declarations by Diagram
  6. 定义水果和方法的java代码_命名水果的两列上的自定义聚合
  7. 技术分析之OGNL表达式概述
  8. 嵌入式系统——文件系统
  9. 阿里Sentinel支持Spring Cloud Gateway的实现
  10. opencv 摄像头基本使用
  11. mac mysql 移动硬盘_mac挂载移动硬盘可以读写的方法
  12. mysql 统计市县的数量
  13. C语言大作业指针字符串处理,[C语言-PTA]说反话-加强版 — 字符串的指针式处理 | 祭夜の咖啡馆...
  14. Git:gnutls_handshake() failed: A TLS packet with unexpected length was received
  15. order by语句使用
  16. Python中的对日期时间的处理
  17. 北京法院京牌小客车司法处置数据统计(Pandas)
  18. [C]你的n元一次常系数线性方程组解答小助手
  19. 用易拉罐自制的“蜘蛛音箱”,效果出乎意料
  20. untracked working tree file已解决

热门文章

  1. python函数count_python中count函数知识点浅析
  2. linux下influxdb安装教程,Linux下安装使用InfluxDB
  3. java宠物小精灵,简单的Java口袋妖怪扑灭模拟器
  4. 苹果手机声音突然变小是怎么回事_苹果7通话声音小,苹果7听筒声音小怎么回事...
  5. Mysql ERROR 1067: Invalid default value for ‘auth_time‘
  6. tomcat错误: javax.management.MalformedObjectNameException: Invalid character ':' in value part of prop
  7. linux中截断日志
  8. spring Boot Actuator使用
  9. select * 映射错误_高性能IO模型分析-浅析Select、Poll、Epoll机制(三)
  10. free技术详解 lock_lock free的理解