1. 程式人生 > >[Swift]最強UIButton解析 | #selector()綁定點擊事件

[Swift]最強UIButton解析 | #selector()綁定點擊事件

led 背景圖片 emf port 獲取字體 events cancel ring 設置圖

函數參數既可以具有名稱(在函數體內使用),也可以具有參數標簽(在調用函數時使用)。

如函數參數標簽和參數名稱中所述。方法參數也是如此,因為方法只是與類型相關聯的函數。

  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 }

[Swift]最強UIButton解析 | #selector()綁定點擊事件