質問編集履歴

1

書式の改善

2016/04/27 08:26

投稿

dynamite_nakaji
dynamite_nakaji

スコア42

test CHANGED
File without changes
test CHANGED
@@ -18,9 +18,131 @@
18
18
 
19
19
  コードは・・・
20
20
 
21
+ class save: UIViewController{
22
+
23
+ var hozon = CustomButton(type: .Custom)
24
+
25
+ var add:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
21
26
 
22
27
 
28
+
29
+ add.hozon.frame = (frame: CGRectMake(145,290,250,250))
30
+
31
+ add.hozon.setBackgroundImage(UIImage(named: "Chiba paper1.png"),forState: UIControlState.Normal)
32
+
33
+ add.hozon.addTarget(self, action: #selector(save.buttonPressed(_:)), forControlEvents:.TouchUpInside)
34
+
35
+ add.hozon.userInteractionEnabled = true
36
+
37
+ add.hozon.tag = 1
38
+
39
+ add.hozon.removeRect = Trash_can.frame
40
+
41
+ view.addSubview(hozon)
42
+
43
+
44
+
45
+ func buttonPressed(sender: UIButton) {
46
+
47
+ if !add.hozon.isMoveing && sender.tag == 1 {
48
+
49
+ view.addSubview(configuration)
50
+
51
+ }
52
+
53
+ class CustomButton: UIButton {
54
+
55
+ var isMoveing : Bool = false
56
+
57
+ var removeRect: CGRect!
58
+
59
+ var position : CGPoint!
60
+
61
+
62
+
63
+ override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
64
+
65
+ super.touchesBegan(touches, withEvent: event)
66
+
67
+ position = self.frame.origin
68
+
69
+ }
70
+
71
+ override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
72
+
73
+ super.touchesMoved(touches, withEvent: event)
74
+
75
+ isMoveing = true
76
+
77
+ let touchEvent = touches.first!
78
+
79
+
80
+
81
+ // ドラッグ前の座標
82
+
83
+ let preDx = touchEvent.previousLocationInView(superview).x
84
+
85
+ let preDy = touchEvent.previousLocationInView(superview).y
86
+
87
+ // ドラッグ後の座標
88
+
89
+ let newDx = touchEvent.locationInView(superview).x
90
+
91
+ let newDy = touchEvent.locationInView(superview).y
92
+
93
+ // ドラッグしたx座標の移動距離
94
+
95
+ let dx = newDx - preDx
96
+
97
+ // ドラッグしたy座標の移動距離
98
+
99
+ let dy = newDy - preDy
100
+
101
+ // 画像のフレーム
102
+
103
+ var viewFrame: CGRect = self.frame
104
+
105
+ // 移動分を反映させる
106
+
107
+ viewFrame.origin.x += dx
108
+
109
+ viewFrame.origin.y += dy
110
+
111
+ self.frame = viewFrame
112
+
113
+ }
114
+
115
+ override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
116
+
117
+ super.touchesEnded(touches, withEvent: event)
118
+
119
+ if let rect = removeRect where CGRectIntersectsRect(self.frame, rect) {
120
+
121
+ self.removeFromSuperview()
122
+
123
+ }else{
124
+
23
- https://teratail.com/questions/32967
125
+ // add.hozon.frame.origin.y = 910
126
+
127
+ // add.hozon.frame.origin.y = 150
128
+
129
+ }
130
+
131
+
132
+
133
+ isMoveing = false
134
+
135
+
136
+
137
+ if position == self.frame.origin {
138
+
139
+ self.sendActionsForControlEvents(.TouchUpInside)}
140
+
141
+ }
142
+
143
+
144
+
145
+
24
146
 
25
147
  を使って作成しています。
26
148