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

   2023-02-09 学习力0
核心提示:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen/LeetCode➤原

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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

  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 }

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • swift 命令行工具初探
    亲爱的同学们好,今天我们要介绍这么一个东西。相信有过解释型语言(PHP,Ruby,等)使用经验的同学会更加熟悉,就是 Swift 也为我们提供了命令行运行工具,俗称 REPL。好了,我们进入正题,在安装好 Swift 开发环境的机器上,打开命令行,输入 swift 命令,就进
    03-16
  • [Swift]冒泡排序 | Bubble sort
    [Swift]冒泡排序 | Bubble sort
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文
    03-08
  • [Swift] 自定义在 SwiftUI 中实现的可搜索的外观
    [Swift] 自定义在 SwiftUI 中实现的可搜索的外
    首先我找遍了,似乎找不到任何信息......(我遇到了许多根本不起作用的事情......)终于在详细的英文文献中找到了,我会保留下来,希望以后有机会。关于 SwiftUI 中的可搜索searchable是iOS15新增的易于实现的搜索字段。关于这种情况有一个参数placement,您
    03-08
  • [Swift] 检测 SwiftUI ScrollView 中的偏移量
    [Swift] 检测 SwiftUI ScrollView 中的偏移量
    首先你可以用ScrollViewReader做一些可以从iOS14使用的事情。但是,我不能做我想做的事情,所以我想我还能做些什么。再次,可重复使用我尝试过了。执行我将首先发布实现的图像。 (Swift Playgrounds 演示)您可以像这样根据滚动获取坐标。让我们看看实现。1.
    03-08
  • Swift3.0 UICollectionView 删除,拖动
    Swift3.0 UICollectionView 删除,拖动
    UICollectionView实现了一下常见的新闻分类.  附有效果图 近期一直在深入学习swift,实现了CollectionView item的头东与删除,用的都是系统的一些函数方法,看起来比较直观. 第一步:class HotViewController: UIViewController,UICollectionViewDelegate,UICo
    02-09
  • swift -懒加载创建view
     // 只有外界访问到headerView的时候才会去执行闭包, 然后将闭包的返回值赋值给headerView    // 注意: 一定要记住闭包后面需要写上(), 代表执行闭包    //懒加载创建UIView    lazy var headerView: UIView = {        let view = UIView()
    02-09
  • Swift--非常好用的适合局部的代码约束
    // 哪个控件的哪个属性等于(大于、小于)另外一个控件的哪个属性乘以多少再加上多少 eg:let widthContraint = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLa
    02-09
  • iOS打包framework - Swift完整项目打包Framework,嵌入OC项目使用
    iOS打包framework - Swift完整项目打包Framewor
    场景说明:-之前做的App,使用Swift框架语言,混合编程,内含少部分OC代码。-需要App整体功能打包成静态库,完整移植到另一个App使用,该App使用OC。-所以涉及到一个语言互转的处理,以及一些AppDelegate的代码减除变化。 --------------------------------
    02-09
  • Swift -- 官方文档Swift-Guides的学习笔记
    在经历的一段时间的郁闷之后,我发现感情都是虚伪的,只有代码是真实的(呸)因为看了swift语法之后依然不会用swift,然后我非常作死的跑去看官方文档,就是xcode里自带的help》documentation and API reference其中的swift里的guide这里主要总结一下里面每一
    02-09
  • Swift - 进度条(UIProgressView)的用法
     1,创建进度条1234var progressView=UIProgressView(progressViewStyle:UIProgressViewStyle.Default)progressView.center=self.view.centerprogressView.progress=0.5 //默认进度50%self.view.addSubview(progressView); 2,设置进度,同时有动画效果1p
    02-09
点击排行