回答編集履歴
1
修正
test
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
+
細かい仕様は分からないので、カスタムボタンを押下した時にタイマーを開始して、3秒後にカスタムボタンが乗っている`ViewController`に`赤いView`を乗せるようにしました。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
カスタムボタンの中で`赤いView`を作成して`superView`の`ViewController`に乗せることもできますが、
|
6
|
+
|
1
|
-
|
7
|
+
Viewの管理が複雑になってしまうので、`ViewController`のメソッドを呼ぶようにして`ViewController`で管理したほうが良いと思います。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
8
|
+
|
6
|
-
|
7
|
-
|
9
|
+
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
このまま貼り付けて実行できるので、動き等確認して参考にしてみてください。
|
12
10
|
|
13
11
|
|
14
12
|
|
@@ -22,15 +20,11 @@
|
|
22
20
|
|
23
21
|
|
24
22
|
|
25
|
-
let
|
23
|
+
let button1 = CustomButton(type: .Custom)
|
26
|
-
|
27
|
-
|
24
|
+
|
28
|
-
|
29
|
-
|
25
|
+
var viewArray: [UIView] = []
|
30
|
-
|
31
|
-
|
26
|
+
|
32
|
-
|
33
|
-
|
27
|
+
|
34
28
|
|
35
29
|
override func viewDidLoad() {
|
36
30
|
|
@@ -38,27 +32,53 @@
|
|
38
32
|
|
39
33
|
|
40
34
|
|
41
|
-
//
|
35
|
+
// ボタン1を生成
|
42
|
-
|
36
|
+
|
43
|
-
|
37
|
+
button1.frame = CGRectMake(100, 100, 200, 50)
|
38
|
+
|
44
|
-
|
39
|
+
button1.setTitle("Button1", forState: .Normal)
|
40
|
+
|
41
|
+
button1.setTitleColor(UIColor.blueColor(), forState: .Normal)
|
42
|
+
|
45
|
-
|
43
|
+
button1.backgroundColor = UIColor.yellowColor()
|
44
|
+
|
46
|
-
|
45
|
+
button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
|
46
|
+
|
47
|
+
button1.userInteractionEnabled = true
|
48
|
+
|
49
|
+
button1.parent = self
|
50
|
+
|
51
|
+
button1.tag = 1
|
52
|
+
|
47
|
-
view.addSubview(
|
53
|
+
view.addSubview(button1)
|
54
|
+
|
48
|
-
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
49
|
-
|
59
|
+
func buttonPressed(sender: UIButton) {
|
60
|
+
|
50
|
-
|
61
|
+
if !button1.isMoveing && sender.tag == 1 {
|
62
|
+
|
51
|
-
//
|
63
|
+
// ボタン1押下時のイベント
|
64
|
+
|
52
|
-
|
65
|
+
print("Push! Button1")
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
func addView() {
|
74
|
+
|
53
|
-
|
75
|
+
let redView = UIView(frame: CGRectMake(CGFloat(10 + (viewArray.count * 55)), 100, 50, 50))
|
54
|
-
|
76
|
+
|
55
|
-
|
77
|
+
redView.backgroundColor = UIColor.redColor()
|
56
|
-
|
57
|
-
|
78
|
+
|
58
|
-
|
59
|
-
viewB.viewAInstance = viewA
|
60
|
-
|
61
|
-
view.addSubview(
|
79
|
+
view.addSubview(redView)
|
80
|
+
|
81
|
+
viewArray.append(redView)
|
62
82
|
|
63
83
|
}
|
64
84
|
|
@@ -66,92 +86,122 @@
|
|
66
86
|
|
67
87
|
|
68
88
|
|
89
|
+
|
90
|
+
|
91
|
+
// CustomButton Class
|
92
|
+
|
93
|
+
class CustomButton: UIButton {
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
var isMoveing: Bool = false
|
98
|
+
|
99
|
+
weak var parent: ViewController?
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
var position: CGPoint!
|
104
|
+
|
105
|
+
var timer = NSTimer()
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
110
|
+
|
111
|
+
super.touchesBegan(touches, withEvent: event)
|
112
|
+
|
113
|
+
position = self.frame.origin
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
120
|
+
|
121
|
+
super.touchesMoved(touches, withEvent: event)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
isMoveing = true
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
let touchEvent = touches.first!
|
130
|
+
|
131
|
+
|
132
|
+
|
69
|
-
//
|
133
|
+
// ドラッグ前の座標
|
134
|
+
|
70
|
-
|
135
|
+
let preDx = touchEvent.previousLocationInView(superview).x
|
136
|
+
|
137
|
+
let preDy = touchEvent.previousLocationInView(superview).y
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
// ドラッグ後の座標
|
142
|
+
|
143
|
+
let newDx = touchEvent.locationInView(superview).x
|
144
|
+
|
145
|
+
let newDy = touchEvent.locationInView(superview).y
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
// ドラッグしたx座標の移動距離
|
150
|
+
|
71
|
-
|
151
|
+
let dx = newDx - preDx
|
152
|
+
|
153
|
+
|
154
|
+
|
72
|
-
|
155
|
+
// ドラッグしたy座標の移動距離
|
156
|
+
|
157
|
+
let dy = newDy - preDy
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
// 画像のフレーム
|
162
|
+
|
73
|
-
var view
|
163
|
+
var viewFrame: CGRect = self.frame
|
164
|
+
|
165
|
+
|
166
|
+
|
74
|
-
|
167
|
+
// 移動分を反映させる
|
168
|
+
|
75
|
-
|
169
|
+
viewFrame.origin.x += dx
|
170
|
+
|
76
|
-
|
171
|
+
viewFrame.origin.y += dy
|
172
|
+
|
173
|
+
self.frame = viewFrame
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
180
|
+
|
181
|
+
super.touchesEnded(touches, withEvent: event)
|
182
|
+
|
183
|
+
isMoveing = false
|
184
|
+
|
185
|
+
if position == self.frame.origin {
|
186
|
+
|
187
|
+
self.sendActionsForControlEvents(.TouchUpInside)
|
188
|
+
|
189
|
+
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(CustomButton.timerAction), userInfo: nil, repeats: false)
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
77
|
-
func
|
197
|
+
func timerAction() {
|
78
|
-
|
79
|
-
|
198
|
+
|
80
|
-
|
81
|
-
|
199
|
+
print("timerAction!")
|
82
|
-
|
200
|
+
|
83
|
-
|
201
|
+
parent?.addView()
|
84
|
-
|
85
|
-
viewArray.append(view)
|
86
202
|
|
87
203
|
}
|
88
204
|
|
89
205
|
}
|
90
206
|
|
91
|
-
|
92
|
-
|
93
|
-
// ViewBクラス
|
94
|
-
|
95
|
-
class ViewB: UIView {
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
var timer = NSTimer()
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
// ViewAのオブジェクトを保持
|
104
|
-
|
105
|
-
var viewAInstance: ViewA?
|
106
|
-
|
107
|
-
var button = UIButton(frame: CGRectMake(20, 20, 120, 50))
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
override init(frame: CGRect) {
|
112
|
-
|
113
|
-
super.init(frame: frame)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
button.setTitle("Timer Start", forState: .Normal)
|
118
|
-
|
119
|
-
button.addTarget(self, action: #selector(ViewB.pushButton), forControlEvents: .TouchUpInside)
|
120
|
-
|
121
|
-
button.backgroundColor = UIColor.blueColor()
|
122
|
-
|
123
|
-
self.addSubview(button)
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
func pushButton() {
|
130
|
-
|
131
|
-
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(ViewB.timerAction), userInfo: nil, repeats: false)
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
func timerAction() {
|
138
|
-
|
139
|
-
viewAInstance?.addView()
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
required init?(coder aDecoder: NSCoder) {
|
146
|
-
|
147
|
-
fatalError("init(coder:) has not been implemented")
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
207
|
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
![image](6d19929e6045b078728a6b1fdf3e9d9c.png)
|