Xcode iOS开发:UIKit常用组件之按钮控件
UIKit框架提供了一系列Class类来创建和管理iOS应用程序的用户界面接口、应用程序对象、事件开头、绘图模型、窗口、视图和用于视图触摸屏等接口。UIKit框架是iOS上的用户图形包,UI开头的类都来自这个框架。
从UIKit中类的结构图中可以了解每个控件的继承顺序,以UIButton按钮控件为例,它在结构图中位于NSObject—>UIResponder—>UIView—>UIControl—>UIButton,从UIButton控件继承类中可以看出,该控件是一种能够响应用户触摸事件的、用户界面显示的类。
UIKit的主要功能包括:
·构建和管理应用程序的用户界面
·捕获触摸和基于移动的交互事件
·呈现文字和web内容
·优化多任务的应用程序
·创建自定义的界面元素
UIButton按钮控件:
UIButton是UIControl的一个子类,它实现了在触摸屏上添加交互按钮的功能,同时UIButton类继承UIView类,这使得UIButton可以在屏幕上显示内容。而UIView继承UIResponder,从而允许按钮响应用户触摸和手势.
下面展示
不同样式的UIButton按钮:
代码(在viewDidLoad方法中创建):
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let buttonTypes:[UIButton.ButtonType]=[.system,.contactAdd,.detailDisclosure,.infoDark,.infoLight,.roundedRect]//按钮样式数组
for i in 0..<buttonTypes.count{//循环生成6种不同样式的按钮
let button = UIButton(type: buttonTypes[i])
button.frame = CGRect(x:100,y:50+i*50,width:120,height: 30)
if i==0||i==5
{
button.setTitle("Button\(i+1)", for: .normal)//设置title标签属性
}
self.view.addSubview(button)
}
}
在viewDidLoad方法中依次创建了六种UIButton按钮,并给第一个和最后一个按钮设置了title标题属性
UIButton按钮不同的状态:
代码(在viewDidLoad方法中创建):
//UI按钮的状态
override func viewDidLoad() {
super.viewDidLoad()
let titleStates:[String]=["正常状态","高亮状态","选中状态","失效状态"]
let buttonStates:[UIControl.State] = [.normal,.highlighted,.selected,.disabled]
let button1 = UIButton(type: .system)
button1.frame = CGRect(x:80,y:50,width: 140,height: 30)//宽高及位置
button1.setTitle(titleStates[0], for: buttonStates[0])//设置title
button1.setBackgroundImage(UIImage(named: "btNormal"), for: UIControl.State.normal)//插入背景图片
self.view.addSubview(button1)
let button2 = UIButton(type: .system)
button2.frame = CGRect(x:80,y:100,width: 140,height: 30)
button2.setTitle(titleStates[1], for: buttonStates[1])
button2.setBackgroundImage(UIImage(named: "btHighlighited"), for: UIControl.State.highlighted)//插入背景图
button2.isHighlighted = true
self.view.addSubview(button2)
let button3 = UIButton(type: .system)
button3.frame = CGRect(x:80,y:150,width: 140,height: 30)
button3.setTitle(titleStates[2], for: buttonStates[2])
button3.setBackgroundImage(UIImage(named:"btSelected"), for: UIControl.State.selected)//背景图
button3.isSelected = true
self.view.addSubview(button3)
let button4 = UIButton(type: .system)
button4.frame = CGRect(x:80,y:200,width: 140,height: 30)
button4.setTitle(titleStates[3], for: buttonStates[3])
button4.setBackgroundImage(UIImage(named: "btDisabled"), for: UIControl.State.disabled)//背景图
button4.isEnabled = false
self.view.addSubview(button4)
}
在viewDidLoad方法中创建了四种不同状态的UIButton按钮,并通过setTitle(_:for:)方法给四种不同状态的按钮设置了不同的标题。
UIButton按钮事件的响应:
通过addTarget(:action:for:)方法指定了响应其触摸事件的target为self,使用#selector选择器设置action为ViewController类的buttonTapped(:)方法,并指定响应事件为UIController枚举中的touchUpinside选项。即当用户点击按钮抬起手指时触发该事件,执行buttonTapped方法。代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let button = UIButton(type: UIButton.ButtonType.roundedRect)
button.frame = CGRect(x: 20, y: 100, width: 280, height: 44)
button.backgroundColor = UIColor.purple
button.tintColor = UIColor.yellow
button.setTitle("Tap Me", for: .normal)
button.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), for: UIControl.Event.touchUpInside)
self.view.addSubview(button)
}
@objc func buttonTapped(_ button:UIButton)//响应方法
{
button.setTitle("The button is tapped.", for: .normal)
button.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
本文地址:https://blog.csdn.net/weixin_43275631/article/details/112762146
上一篇: codeforces 补题总结 2
下一篇: 使用Sphinx对索引进行搜索