swift 触摸事件和按钮点击事件能同时存在吗
1个回答
展开全部
写了一个三按钮共用一个click函数的方式,click函数会捕捉到上面三个按钮的UIButton 对象,并访问其中的.tag属性,代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class ActionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//红按钮
let redButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
redButton.frame = CGRectMake(0, 20, 100, 30)
redButton.setTitle("点我变红", forState: UIControlState.Normal)
redButton.backgroundColor = UIColor.redColor()
redButton.tag = 100
self.view.addSubview(redButton)
//蓝色按钮
let blueButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
blueButton.frame = CGRectMake(0, 60, 100, 30)
blueButton.setTitle("点我变蓝", forState: UIControlState.Normal)
blueButton.backgroundColor = UIColor.blueColor()
blueButton.tag = 101
self.view.addSubview(blueButton)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class ActionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//红按钮
let redButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
redButton.frame = CGRectMake(0, 20, 100, 30)
redButton.setTitle("点我变红", forState: UIControlState.Normal)
redButton.backgroundColor = UIColor.redColor()
redButton.tag = 100
self.view.addSubview(redButton)
//蓝色按钮
let blueButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
blueButton.frame = CGRectMake(0, 60, 100, 30)
blueButton.setTitle("点我变蓝", forState: UIControlState.Normal)
blueButton.backgroundColor = UIColor.blueColor()
blueButton.tag = 101
self.view.addSubview(blueButton)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询