回答編集履歴

2

修正

2016/04/21 16:03

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- ボタン押すと左に赤いViewがaddされます、そしてUIButtonはドラッグで移動できます。
13
+ ボタン1とボタン2がありそれぞれ押すと左に黄色、緑色のViewがaddされます、そしてUIButtonはドラッグで移動できます。
14
14
 
15
15
 
16
16
 
@@ -26,9 +26,13 @@
26
26
 
27
27
  var viewCount: CGFloat = 0
28
28
 
29
+
30
+
29
- let button = CustomButton(type: .Custom)
31
+ let button1 = CustomButton(type: .Custom)
32
+
30
-
33
+ let button2 = CustomButton(type: .Custom)
31
-
34
+
35
+
32
36
 
33
37
  override func viewDidLoad() {
34
38
 
@@ -36,25 +40,43 @@
36
40
 
37
41
 
38
42
 
39
- // ボタンを生成
43
+ // ボタン1を生成
40
-
44
+
41
- button.frame = CGRectMake(100, 100, 200, 50)
45
+ button1.frame = CGRectMake(100, 100, 200, 50)
42
-
46
+
43
- button.setTitle("Button", forState: .Normal)
47
+ button1.setTitle("Button1", forState: .Normal)
44
-
48
+
45
- button.setTitleColor(UIColor.blueColor(), forState: .Normal)
49
+ button1.setTitleColor(UIColor.blueColor(), forState: .Normal)
46
-
50
+
47
- button.backgroundColor = UIColor.yellowColor()
51
+ button1.backgroundColor = UIColor.yellowColor()
48
-
52
+
49
- button.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
53
+ button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
50
-
54
+
51
- button.userInteractionEnabled = true
55
+ button1.userInteractionEnabled = true
52
-
56
+
53
- button.tag = 999
57
+ button1.tag = 1
54
-
55
- button.vc = self
58
+
56
-
57
- view.addSubview(button)
59
+ view.addSubview(button1)
60
+
61
+
62
+
63
+ // ボタン2を生成
64
+
65
+ button2.frame = CGRectMake(100, 200, 200, 50)
66
+
67
+ button2.setTitle("Button2", forState: .Normal)
68
+
69
+ button2.setTitleColor(UIColor.blueColor(), forState: .Normal)
70
+
71
+ button2.backgroundColor = UIColor.greenColor()
72
+
73
+ button2.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
74
+
75
+ button2.userInteractionEnabled = true
76
+
77
+ button2.tag = 2
78
+
79
+ view.addSubview(button2)
58
80
 
59
81
  }
60
82
 
@@ -62,152 +84,148 @@
62
84
 
63
85
  func buttonPressed(sender: UIButton) {
64
86
 
65
-
66
-
67
- if !button.isMoveing {
87
+ if !button1.isMoveing && sender.tag == 1 {
68
-
88
+
69
- // 赤いViewを画面に追加
89
+ // ボタン1押下時のイベント
70
-
71
- let v = UIView(frame: CGRectMake(10, 10 + viewCount * 40 , 30, 30))
90
+
72
-
73
- v.backgroundColor = UIColor.redColor()
91
+ generateLabel("1", color: UIColor.yellowColor())
74
-
75
- view.addSubview(v)
76
-
77
- viewCount += 1
78
92
 
79
93
  }
80
94
 
95
+
96
+
97
+ if !button2.isMoveing && sender.tag == 2 {
98
+
99
+ // ボタン2押下時のイベント
100
+
101
+ generateLabel("2", color: UIColor.greenColor())
102
+
81
- }
103
+ }
104
+
82
-
105
+ }
106
+
107
+
108
+
83
-
109
+ func generateLabel(text: String, color: UIColor) {
110
+
111
+ let v = UILabel(frame: CGRectMake(10, 10 + viewCount * 40 , 30, 30))
112
+
113
+ v.text = text
114
+
115
+ v.textAlignment = .Center
116
+
117
+ v.backgroundColor = color
118
+
119
+ view.addSubview(v)
120
+
121
+ viewCount += 1
122
+
123
+ }
124
+
125
+ }
126
+
127
+
128
+
129
+
130
+
131
+ // CustomButton Class
132
+
133
+ class CustomButton: UIButton {
134
+
135
+
136
+
137
+ var isMoveing: Bool = false
138
+
139
+ var position: CGPoint!
140
+
141
+
142
+
143
+ override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
144
+
145
+ super.touchesBegan(touches, withEvent: event)
146
+
147
+ position = self.frame.origin
148
+
149
+ }
84
150
 
85
151
 
86
152
 
87
153
  override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
88
154
 
89
-
155
+ super.touchesMoved(touches, withEvent: event)
90
-
156
+
157
+
158
+
91
- // タッチイベントを取得
159
+ isMoveing = true
160
+
161
+
92
162
 
93
163
  let touchEvent = touches.first!
94
164
 
165
+
166
+
167
+ // ドラッグ前の座標
168
+
169
+ let preDx = touchEvent.previousLocationInView(superview).x
170
+
171
+ let preDy = touchEvent.previousLocationInView(superview).y
172
+
173
+
174
+
175
+ // ドラッグ後の座標
176
+
95
- let tag = touchEvent.view?.tag
177
+ let newDx = touchEvent.locationInView(superview).x
178
+
96
-
179
+ let newDy = touchEvent.locationInView(superview).y
180
+
181
+
182
+
97
-
183
+ // ドラッグしたx座標の移動距離
184
+
98
-
185
+ let dx = newDx - preDx
186
+
187
+
188
+
189
+ // ドラッグしたy座標の移動距離
190
+
191
+ let dy = newDy - preDy
192
+
193
+
194
+
195
+ // 画像のフレーム
196
+
197
+ var viewFrame: CGRect = self.frame
198
+
199
+
200
+
201
+ // 移動分を反映させる
202
+
203
+ viewFrame.origin.x += dx
204
+
205
+ viewFrame.origin.y += dy
206
+
207
+ self.frame = viewFrame
208
+
209
+ }
210
+
211
+
212
+
213
+ override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
214
+
215
+ super.touchesEnded(touches, withEvent: event)
216
+
99
- if tag == 999 {
217
+ isMoveing = false
218
+
100
-
219
+ if position == self.frame.origin {
220
+
101
- moveView(touchEvent, vi: button)
221
+ self.sendActionsForControlEvents(.TouchUpInside)
102
222
 
103
223
  }
104
224
 
105
225
  }
106
226
 
107
-
108
-
109
-
110
-
111
- func moveView<T: UIView>(touchEvent: UITouch, vi: T) {
112
-
113
- // ドラッグ前の座標
114
-
115
- let preDx = touchEvent.previousLocationInView(self.view).x
116
-
117
- let preDy = touchEvent.previousLocationInView(self.view).y
118
-
119
-
120
-
121
- // ドラッグ後の座標
122
-
123
- let newDx = touchEvent.locationInView(self.view).x
124
-
125
- let newDy = touchEvent.locationInView(self.view).y
126
-
127
-
128
-
129
- // ドラッグしたx座標の移動距離
130
-
131
- let dx = newDx - preDx
132
-
133
-
134
-
135
- // ドラッグしたy座標の移動距離
136
-
137
- let dy = newDy - preDy
138
-
139
-
140
-
141
- // 画像のフレーム
142
-
143
- var viewFrame: CGRect = vi.frame
144
-
145
-
146
-
147
- // 移動分を反映させる
148
-
149
- viewFrame.origin.x += dx
150
-
151
- viewFrame.origin.y += dy
152
-
153
- vi.frame = viewFrame
154
-
155
- }
156
-
157
-
158
-
159
- override func didReceiveMemoryWarning() {
160
-
161
- super.didReceiveMemoryWarning()
162
-
163
-
164
-
165
- }
166
-
167
227
  }
168
228
 
169
-
170
-
171
-
172
-
173
- // CustomButton Class
174
-
175
- class CustomButton: UIButton {
176
-
177
-
178
-
179
- var vc: UIViewController!
180
-
181
- var isMoveing: Bool = false
182
-
183
-
184
-
185
- override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
186
-
187
- super.touchesMoved(touches, withEvent: event)
188
-
189
- isMoveing = true
190
-
191
- vc.touchesMoved(touches, withEvent: event)
192
-
193
- }
194
-
195
-
196
-
197
- override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
198
-
199
- super.touchesEnded(touches, withEvent: event)
200
-
201
- isMoveing = false
202
-
203
- vc.touchesEnded(touches, withEvent: event)
204
-
205
- }
206
-
207
- }
208
-
209
229
  ```
210
230
 
211
-
212
-
213
- ![image](0c3411f1a8e4f5a745bc6ea65aa36490.png)
231
+ ![image](16bb305b1d24dbac219aaf207bee24e2.png)

1

追記

2016/04/21 16:03

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
 
13
+ ボタンを押すと左に赤いViewがaddされます、そしてUIButtonはドラッグで移動できます。
14
+
15
+
16
+
13
17
  ```swift
14
18
 
15
19
  import UIKit