回答編集履歴
1
修正
answer
CHANGED
@@ -1,79 +1,104 @@
|
|
1
|
-
|
1
|
+
細かい仕様は分からないので、カスタムボタンを押下した時にタイマーを開始して、3秒後にカスタムボタンが乗っている`ViewController`に`赤いView`を乗せるようにしました。
|
2
2
|
|
3
|
-
AppDelegateを経由しなくても、この程度の事でしたら実現できます。
|
4
|
-
|
3
|
+
カスタムボタンの中で`赤いView`を作成して`superView`の`ViewController`に乗せることもできますが、
|
4
|
+
Viewの管理が複雑になってしまうので、`ViewController`のメソッドを呼ぶようにして`ViewController`で管理したほうが良いと思います。
|
5
5
|
|
6
|
-
このまま貼り付けて実行できるので、動き等確認して参考にしてみてください。
|
7
6
|
|
8
7
|
```swift
|
9
8
|
import UIKit
|
10
9
|
|
11
10
|
class ViewController: UIViewController {
|
12
11
|
|
13
|
-
let
|
12
|
+
let button1 = CustomButton(type: .Custom)
|
14
|
-
// ViewA,ViewB両方のオブジェクトを保持
|
15
|
-
|
13
|
+
var viewArray: [UIView] = []
|
16
|
-
|
14
|
+
|
17
|
-
|
18
15
|
override func viewDidLoad() {
|
19
16
|
super.viewDidLoad()
|
20
17
|
|
21
|
-
//
|
18
|
+
// ボタン1を生成
|
22
|
-
|
19
|
+
button1.frame = CGRectMake(100, 100, 200, 50)
|
20
|
+
button1.setTitle("Button1", forState: .Normal)
|
21
|
+
button1.setTitleColor(UIColor.blueColor(), forState: .Normal)
|
23
|
-
|
22
|
+
button1.backgroundColor = UIColor.yellowColor()
|
23
|
+
button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
|
24
|
+
button1.userInteractionEnabled = true
|
25
|
+
button1.parent = self
|
26
|
+
button1.tag = 1
|
24
|
-
view.addSubview(
|
27
|
+
view.addSubview(button1)
|
25
|
-
|
26
|
-
// viewBを生成
|
27
|
-
viewB.frame = CGRectMake(20, 300, viewWidth - 20 * 2, 100)
|
28
|
-
viewB.backgroundColor = UIColor.greenColor()
|
29
|
-
//ViewAのオブジェクトをViewBのプロパティに設定
|
30
|
-
viewB.viewAInstance = viewA
|
31
|
-
view.addSubview(viewB)
|
32
28
|
}
|
33
|
-
}
|
34
|
-
|
35
|
-
// ViewAクラス
|
36
|
-
class ViewA: UIView {
|
37
|
-
var viewArray: [UIView] = []
|
38
29
|
|
30
|
+
func buttonPressed(sender: UIButton) {
|
31
|
+
if !button1.isMoveing && sender.tag == 1 {
|
32
|
+
// ボタン1押下時のイベント
|
33
|
+
print("Push! Button1")
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
39
37
|
func addView() {
|
40
|
-
let
|
38
|
+
let redView = UIView(frame: CGRectMake(CGFloat(10 + (viewArray.count * 55)), 100, 50, 50))
|
41
|
-
|
39
|
+
redView.backgroundColor = UIColor.redColor()
|
42
|
-
|
40
|
+
view.addSubview(redView)
|
43
|
-
viewArray.append(
|
41
|
+
viewArray.append(redView)
|
44
42
|
}
|
45
43
|
}
|
46
44
|
|
45
|
+
|
47
|
-
//
|
46
|
+
// CustomButton Class
|
48
|
-
class
|
47
|
+
class CustomButton: UIButton {
|
49
48
|
|
49
|
+
var isMoveing: Bool = false
|
50
|
+
weak var parent: ViewController?
|
51
|
+
|
52
|
+
var position: CGPoint!
|
50
53
|
var timer = NSTimer()
|
51
54
|
|
52
|
-
// ViewAのオブジェクトを保持
|
53
|
-
|
55
|
+
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
54
|
-
|
56
|
+
super.touchesBegan(touches, withEvent: event)
|
57
|
+
position = self.frame.origin
|
58
|
+
}
|
55
59
|
|
60
|
+
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
56
|
-
|
61
|
+
super.touchesMoved(touches, withEvent: event)
|
57
|
-
super.init(frame: frame)
|
58
62
|
|
63
|
+
isMoveing = true
|
64
|
+
|
65
|
+
let touchEvent = touches.first!
|
66
|
+
|
67
|
+
// ドラッグ前の座標
|
59
|
-
|
68
|
+
let preDx = touchEvent.previousLocationInView(superview).x
|
60
|
-
|
69
|
+
let preDy = touchEvent.previousLocationInView(superview).y
|
70
|
+
|
71
|
+
// ドラッグ後の座標
|
61
|
-
|
72
|
+
let newDx = touchEvent.locationInView(superview).x
|
73
|
+
let newDy = touchEvent.locationInView(superview).y
|
74
|
+
|
75
|
+
// ドラッグしたx座標の移動距離
|
76
|
+
let dx = newDx - preDx
|
77
|
+
|
78
|
+
// ドラッグしたy座標の移動距離
|
79
|
+
let dy = newDy - preDy
|
80
|
+
|
81
|
+
// 画像のフレーム
|
82
|
+
var viewFrame: CGRect = self.frame
|
83
|
+
|
84
|
+
// 移動分を反映させる
|
85
|
+
viewFrame.origin.x += dx
|
86
|
+
viewFrame.origin.y += dy
|
62
|
-
self.
|
87
|
+
self.frame = viewFrame
|
63
88
|
}
|
64
89
|
|
90
|
+
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
91
|
+
super.touchesEnded(touches, withEvent: event)
|
65
|
-
|
92
|
+
isMoveing = false
|
93
|
+
if position == self.frame.origin {
|
94
|
+
self.sendActionsForControlEvents(.TouchUpInside)
|
66
|
-
|
95
|
+
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(CustomButton.timerAction), userInfo: nil, repeats: false)
|
96
|
+
}
|
67
97
|
}
|
68
98
|
|
69
|
-
func timerAction()
|
99
|
+
func timerAction() {
|
100
|
+
print("timerAction!")
|
70
|
-
|
101
|
+
parent?.addView()
|
71
102
|
}
|
72
|
-
|
73
|
-
required init?(coder aDecoder: NSCoder) {
|
74
|
-
fatalError("init(coder:) has not been implemented")
|
75
|
-
}
|
76
103
|
}
|
77
|
-
```
|
104
|
+
```
|
78
|
-
|
79
|
-

|