質問編集履歴

1

コードの追加

2016/06/29 20:05

投稿

hirotoshin
hirotoshin

スコア24

test CHANGED
File without changes
test CHANGED
@@ -2,4 +2,94 @@
2
2
 
3
3
  ボタンを一回タップするとテキストビューの背景が赤に変わり、二回タップするとテキストビューの色が青に変わるプログラムを実装したいと考えています。
4
4
 
5
+ 現在は自分で作ったrectをタップしたら色が変わるプログラムとなっています。
6
+
5
- 分かる方がいましたら是非ともご教授お願いしたいです。
7
+ これをボタンで色を変化させる方法が分かる方がいましたら是非ともご教授お願いしたいです。
8
+
9
+
10
+
11
+ import UIKit
12
+
13
+
14
+
15
+ class ViewController: UIViewController {
16
+
17
+ var view1: UIView!
18
+
19
+
20
+
21
+ override func viewDidLoad() {
22
+
23
+
24
+
25
+ super.viewDidLoad()
26
+
27
+ // Do any additional setup after loading the view, typically from a nib.
28
+
29
+
30
+
31
+ // Playground - noun: a place where people can play
32
+
33
+ self.view.backgroundColor = .whiteColor()
34
+
35
+
36
+
37
+ // ビューの生成
38
+
39
+ view1 = UIView(frame: CGRectMake(30, 30, 100, 100))
40
+
41
+ view1.backgroundColor = .redColor()
42
+
43
+
44
+
45
+ // ジェスチャーの生成
46
+
47
+ let aSelector = #selector(ViewController.tapGesture(_:))
48
+
49
+ let tapRecognizer = UITapGestureRecognizer(target: self, action: aSelector)
50
+
51
+ // ジェスチャーの追加
52
+
53
+ view1.addGestureRecognizer(tapRecognizer)
54
+
55
+ self.view.addSubview(view1)
56
+
57
+ }
58
+
59
+
60
+
61
+
62
+
63
+ func tapGesture(gestureRecognizer: UITapGestureRecognizer){
64
+
65
+ // タップviewの色を変える (Red <=> Blue)
66
+
67
+ if(view1.backgroundColor == .redColor()) {
68
+
69
+ view1.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.2)
70
+
71
+ }else if(view1.backgroundColor == .blueColor()){
72
+
73
+ view1.backgroundColor = .whiteColor()
74
+
75
+ }else if (view1.backgroundColor == .whiteColor()){
76
+
77
+ view1.backgroundColor = .redColor()
78
+
79
+ }
80
+
81
+ }
82
+
83
+
84
+
85
+ override func didReceiveMemoryWarning() {
86
+
87
+ super.didReceiveMemoryWarning()
88
+
89
+ // Dispose of any resources that can be recreated.
90
+
91
+ }
92
+
93
+
94
+
95
+ }