回答編集履歴
2
修正
answer
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
要件を満たしているか分かりませんが、ここからアレンジすればやりたい事ができるようになると思います。
|
5
5
|
下記のコードをViewControllerにそのまま貼り付けると動くと思います。
|
6
6
|
|
7
|
-
ボタン
|
7
|
+
ボタン1とボタン2がありそれぞれ押すと左に黄色、緑色のViewがaddされます、そしてUIButtonはドラッグで移動できます。
|
8
8
|
|
9
9
|
```swift
|
10
10
|
import UIKit
|
@@ -12,55 +12,82 @@
|
|
12
12
|
class ViewController: UIViewController {
|
13
13
|
|
14
14
|
var viewCount: CGFloat = 0
|
15
|
+
|
15
|
-
let
|
16
|
+
let button1 = CustomButton(type: .Custom)
|
16
|
-
|
17
|
+
let button2 = CustomButton(type: .Custom)
|
18
|
+
|
17
19
|
override func viewDidLoad() {
|
18
20
|
super.viewDidLoad()
|
19
21
|
|
20
|
-
// ボタンを生成
|
22
|
+
// ボタン1を生成
|
21
|
-
|
23
|
+
button1.frame = CGRectMake(100, 100, 200, 50)
|
22
|
-
|
24
|
+
button1.setTitle("Button1", forState: .Normal)
|
23
|
-
|
25
|
+
button1.setTitleColor(UIColor.blueColor(), forState: .Normal)
|
24
|
-
|
26
|
+
button1.backgroundColor = UIColor.yellowColor()
|
25
|
-
|
27
|
+
button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
|
26
|
-
|
28
|
+
button1.userInteractionEnabled = true
|
27
|
-
|
29
|
+
button1.tag = 1
|
28
|
-
button.vc = self
|
29
|
-
view.addSubview(
|
30
|
+
view.addSubview(button1)
|
31
|
+
|
32
|
+
// ボタン2を生成
|
33
|
+
button2.frame = CGRectMake(100, 200, 200, 50)
|
34
|
+
button2.setTitle("Button2", forState: .Normal)
|
35
|
+
button2.setTitleColor(UIColor.blueColor(), forState: .Normal)
|
36
|
+
button2.backgroundColor = UIColor.greenColor()
|
37
|
+
button2.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
|
38
|
+
button2.userInteractionEnabled = true
|
39
|
+
button2.tag = 2
|
40
|
+
view.addSubview(button2)
|
30
41
|
}
|
31
42
|
|
32
43
|
func buttonPressed(sender: UIButton) {
|
44
|
+
if !button1.isMoveing && sender.tag == 1 {
|
45
|
+
// ボタン1押下時のイベント
|
46
|
+
generateLabel("1", color: UIColor.yellowColor())
|
47
|
+
}
|
33
48
|
|
34
|
-
if !
|
49
|
+
if !button2.isMoveing && sender.tag == 2 {
|
35
|
-
//
|
50
|
+
// ボタン2押下時のイベント
|
36
|
-
let v = UIView(frame: CGRectMake(10, 10 + viewCount * 40 , 30, 30))
|
37
|
-
|
51
|
+
generateLabel("2", color: UIColor.greenColor())
|
38
|
-
view.addSubview(v)
|
39
|
-
viewCount += 1
|
40
52
|
}
|
41
53
|
}
|
42
54
|
|
55
|
+
func generateLabel(text: String, color: UIColor) {
|
56
|
+
let v = UILabel(frame: CGRectMake(10, 10 + viewCount * 40 , 30, 30))
|
57
|
+
v.text = text
|
58
|
+
v.textAlignment = .Center
|
59
|
+
v.backgroundColor = color
|
60
|
+
view.addSubview(v)
|
61
|
+
viewCount += 1
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
// CustomButton Class
|
67
|
+
class CustomButton: UIButton {
|
43
68
|
|
69
|
+
var isMoveing: Bool = false
|
70
|
+
var position: CGPoint!
|
71
|
+
|
72
|
+
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
73
|
+
super.touchesBegan(touches, withEvent: event)
|
74
|
+
position = self.frame.origin
|
75
|
+
}
|
76
|
+
|
44
77
|
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
78
|
+
super.touchesMoved(touches, withEvent: event)
|
45
79
|
|
46
|
-
|
80
|
+
isMoveing = true
|
81
|
+
|
47
82
|
let touchEvent = touches.first!
|
48
|
-
let tag = touchEvent.view?.tag
|
49
83
|
|
50
|
-
if tag == 999 {
|
51
|
-
moveView(touchEvent, vi: button)
|
52
|
-
}
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
func moveView<T: UIView>(touchEvent: UITouch, vi: T) {
|
57
84
|
// ドラッグ前の座標
|
58
|
-
let preDx = touchEvent.previousLocationInView(
|
85
|
+
let preDx = touchEvent.previousLocationInView(superview).x
|
59
|
-
let preDy = touchEvent.previousLocationInView(
|
86
|
+
let preDy = touchEvent.previousLocationInView(superview).y
|
60
87
|
|
61
88
|
// ドラッグ後の座標
|
62
|
-
let newDx = touchEvent.locationInView(
|
89
|
+
let newDx = touchEvent.locationInView(superview).x
|
63
|
-
let newDy = touchEvent.locationInView(
|
90
|
+
let newDy = touchEvent.locationInView(superview).y
|
64
91
|
|
65
92
|
// ドラッグしたx座標の移動距離
|
66
93
|
let dx = newDx - preDx
|
@@ -69,39 +96,21 @@
|
|
69
96
|
let dy = newDy - preDy
|
70
97
|
|
71
98
|
// 画像のフレーム
|
72
|
-
var viewFrame: CGRect =
|
99
|
+
var viewFrame: CGRect = self.frame
|
73
100
|
|
74
101
|
// 移動分を反映させる
|
75
102
|
viewFrame.origin.x += dx
|
76
103
|
viewFrame.origin.y += dy
|
77
|
-
|
104
|
+
self.frame = viewFrame
|
78
105
|
}
|
79
|
-
|
80
|
-
override func didReceiveMemoryWarning() {
|
81
|
-
super.didReceiveMemoryWarning()
|
82
|
-
|
83
|
-
}
|
84
|
-
}
|
85
|
-
|
86
|
-
|
87
|
-
// CustomButton Class
|
88
|
-
class CustomButton: UIButton {
|
89
106
|
|
90
|
-
var vc: UIViewController!
|
91
|
-
var isMoveing: Bool = false
|
92
|
-
|
93
|
-
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
94
|
-
super.touchesMoved(touches, withEvent: event)
|
95
|
-
isMoveing = true
|
96
|
-
vc.touchesMoved(touches, withEvent: event)
|
97
|
-
}
|
98
|
-
|
99
107
|
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
100
108
|
super.touchesEnded(touches, withEvent: event)
|
101
109
|
isMoveing = false
|
110
|
+
if position == self.frame.origin {
|
102
|
-
|
111
|
+
self.sendActionsForControlEvents(.TouchUpInside)
|
112
|
+
}
|
103
113
|
}
|
104
114
|
}
|
105
115
|
```
|
106
|
-
|
107
|
-

|
1
追記
answer
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
要件を満たしているか分かりませんが、ここからアレンジすればやりたい事ができるようになると思います。
|
5
5
|
下記のコードをViewControllerにそのまま貼り付けると動くと思います。
|
6
6
|
|
7
|
+
ボタンを押すと左に赤いViewがaddされます、そしてUIButtonはドラッグで移動できます。
|
8
|
+
|
7
9
|
```swift
|
8
10
|
import UIKit
|
9
11
|
|