teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2016/05/04 09:57

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,79 +1,104 @@
1
- ベース`ViewController`に`ViewA`と`ViewB`のオブジェクトをのせて`ViewB`の`Timer Start`のボタンを押下すると3秒後に`ViewA`に`赤いView`乗る様な簡単なプログラムを書いてみました。
1
+ 細かい仕様は分からないカスタムボタンを押下した時にタイマーを開始して、3秒後にカスタムボタンが乗っている`ViewController`に`赤いView`ようにしました。
2
2
 
3
- AppDelegateを経由しなくても、この程度の事でしたら実現できます。
4
- ※AppDelegateは便利屋はないので、何でもAppDelegate詰め込むのは良くない思います
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 viewWidth = UIScreen.mainScreen().bounds.size.width
12
+ let button1 = CustomButton(type: .Custom)
14
- // ViewA,ViewB両方のオブジェクトを保持
15
- let viewA = ViewA()
13
+ var viewArray: [UIView] = []
16
- let viewB = ViewB()
14
+
17
-
18
15
  override func viewDidLoad() {
19
16
  super.viewDidLoad()
20
17
 
21
- // viewAを生成
18
+ // ボタン1を生成
22
- viewA.frame = CGRectMake(20, 100, viewWidth - 20 * 2, 100)
19
+ button1.frame = CGRectMake(100, 100, 200, 50)
20
+ button1.setTitle("Button1", forState: .Normal)
21
+ button1.setTitleColor(UIColor.blueColor(), forState: .Normal)
23
- viewA.backgroundColor = UIColor.yellowColor()
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(viewA)
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 view = UIView(frame: CGRectMake(CGFloat(10 + (viewArray.count * 55)), 10, 50, 50))
38
+ let redView = UIView(frame: CGRectMake(CGFloat(10 + (viewArray.count * 55)), 100, 50, 50))
41
- view.backgroundColor = UIColor.redColor()
39
+ redView.backgroundColor = UIColor.redColor()
42
- self.addSubview(view)
40
+ view.addSubview(redView)
43
- viewArray.append(view)
41
+ viewArray.append(redView)
44
42
  }
45
43
  }
46
44
 
45
+
47
- // ViewBクラス
46
+ // CustomButton Class
48
- class ViewB: UIView {
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
- var viewAInstance: ViewA?
55
+ override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
54
- var button = UIButton(frame: CGRectMake(20, 20, 120, 50))
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
- override init(frame: CGRect) {
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
- button.setTitle("Timer Start", forState: .Normal)
68
+ let preDx = touchEvent.previousLocationInView(superview).x
60
- button.addTarget(self, action: #selector(ViewB.pushButton), forControlEvents: .TouchUpInside)
69
+ let preDy = touchEvent.previousLocationInView(superview).y
70
+
71
+ // ドラッグ後の座標
61
- button.backgroundColor = UIColor.blueColor()
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.addSubview(button)
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
- func pushButton() {
92
+ isMoveing = false
93
+ if position == self.frame.origin {
94
+ self.sendActionsForControlEvents(.TouchUpInside)
66
- timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(ViewB.timerAction), userInfo: nil, repeats: false)
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
- viewAInstance?.addView()
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
- ![image](6d19929e6045b078728a6b1fdf3e9d9c.png)