回答編集履歴

3

不要なコードの削除

2017/11/09 13:50

投稿

ndo
ndo

スコア115

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pannedFromA(sender:)))
36
36
 
37
- self.view.isUserInteractionEnabled = true
37
+
38
38
 
39
39
  buttonA.addGestureRecognizer(panGestureRecognizer)
40
40
 

2

質問を読み違えていたため、回答を大きく修正

2017/11/09 13:50

投稿

ndo
ndo

スコア115

test CHANGED
@@ -1,33 +1,71 @@
1
- ご認識の通り最初にButtonBの範囲外触れドラッグしてもButtonBのイベントは起きません。
1
+ ※質問読み違えいたため回答を大く修正しした
2
2
 
3
3
 
4
4
 
5
- そこでButtonATouchUpOutsideをハドリングし、指をはなした座標がButtonBの座標の範囲内にあるかどうかで判断します。
5
+ UIButtonのアクショはなく、UIPanGestureRecognizerを使います。
6
6
 
7
7
 
8
8
 
9
9
  ```swift
10
10
 
11
- // ButtonAのTouchUpOutsideアクション
11
+ import UIKit
12
12
 
13
- @IBAction func touchedUpAOutside(_ sender: Any, forEvent event: UIEvent) {
14
13
 
15
- print("ここはButtonAのアクション")
16
14
 
15
+ class ViewController: UIViewController {
16
+
17
+
18
+
19
+ @IBOutlet weak var buttonA: UIButton!
20
+
21
+ @IBOutlet weak var buttonB: UIButton!
22
+
23
+
24
+
25
+ var isInButtonB = false;
26
+
27
+
28
+
29
+ override func viewDidLoad() {
30
+
31
+ super.viewDidLoad()
32
+
33
+
34
+
35
+ let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pannedFromA(sender:)))
36
+
37
+ self.view.isUserInteractionEnabled = true
38
+
39
+ buttonA.addGestureRecognizer(panGestureRecognizer)
40
+
41
+ }
42
+
43
+
44
+
45
+ @objc func pannedFromA(sender: UIPanGestureRecognizer){
46
+
47
+
48
+
17
- if let location = event.allTouches?.first?.location(in: self.view), buttonB.frame.contains(location) {
49
+ if buttonB.frame.contains(sender.location(in: self.view)) {
50
+
51
+ if !isInButtonB { // ←ButtonBの外側からドラッグしてきた場合のみ
52
+
53
+ print("ButtonBのに触れたら処理実行")
54
+
55
+ }
56
+
57
+ isInButtonB = true
18
58
 
19
59
 
20
60
 
21
- print("ButtonBの座標の範囲内なら処理実行")
61
+ } else { // ButtonBの内側でドラッグしても何もし
62
+
63
+ isInButtonB = false
22
64
 
23
65
  }
24
66
 
25
67
  }
26
68
 
69
+ }
70
+
27
71
  ```
28
-
29
-
30
-
31
- コメントに対する補足
32
-
33
- ![コメントに対する補足](80fa87def7803d3dcee0aa667973216b.png)

1

質問に対する補足

2017/11/08 14:34

投稿

ndo
ndo

スコア115

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  // ButtonAのTouchUpOutsideアクション
12
12
 
13
- @IBAction func touchedUpOutside(_ sender: Any, forEvent event: UIEvent) {
13
+ @IBAction func touchedUpAOutside(_ sender: Any, forEvent event: UIEvent) {
14
14
 
15
15
  print("ここはButtonAのアクション")
16
16
 
@@ -25,3 +25,9 @@
25
25
  }
26
26
 
27
27
  ```
28
+
29
+
30
+
31
+ コメントに対する補足
32
+
33
+ ![コメントに対する補足](80fa87def7803d3dcee0aa667973216b.png)