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

質問編集履歴

1

書式の改善

2016/04/27 08:26

投稿

dynamite_nakaji
dynamite_nakaji

スコア42

title CHANGED
File without changes
body CHANGED
@@ -8,8 +8,69 @@
8
8
  そこでボタンをデリゲートに入れたのですが、エラーがでます。
9
9
 
10
10
  コードは・・・
11
+ class save: UIViewController{
12
+ var hozon = CustomButton(type: .Custom)
13
+ var add:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
11
14
 
15
+ add.hozon.frame = (frame: CGRectMake(145,290,250,250))
16
+ add.hozon.setBackgroundImage(UIImage(named: "Chiba paper1.png"),forState: UIControlState.Normal)
17
+ add.hozon.addTarget(self, action: #selector(save.buttonPressed(_:)), forControlEvents:.TouchUpInside)
18
+ add.hozon.userInteractionEnabled = true
19
+ add.hozon.tag = 1
20
+ add.hozon.removeRect = Trash_can.frame
21
+ view.addSubview(hozon)
22
+
23
+ func buttonPressed(sender: UIButton) {
24
+ if !add.hozon.isMoveing && sender.tag == 1 {
25
+ view.addSubview(configuration)
26
+ }
27
+ class CustomButton: UIButton {
28
+ var isMoveing : Bool = false
29
+ var removeRect: CGRect!
30
+ var position : CGPoint!
31
+
32
+ override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
33
+ super.touchesBegan(touches, withEvent: event)
34
+ position = self.frame.origin
35
+ }
36
+ override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
37
+ super.touchesMoved(touches, withEvent: event)
38
+ isMoveing = true
39
+ let touchEvent = touches.first!
40
+
41
+ // ドラッグ前の座標
42
+ let preDx = touchEvent.previousLocationInView(superview).x
43
+ let preDy = touchEvent.previousLocationInView(superview).y
44
+ // ドラッグ後の座標
45
+ let newDx = touchEvent.locationInView(superview).x
46
+ let newDy = touchEvent.locationInView(superview).y
47
+ // ドラッグしたx座標の移動距離
48
+ let dx = newDx - preDx
49
+ // ドラッグしたy座標の移動距離
50
+ let dy = newDy - preDy
51
+ // 画像のフレーム
52
+ var viewFrame: CGRect = self.frame
53
+ // 移動分を反映させる
54
+ viewFrame.origin.x += dx
55
+ viewFrame.origin.y += dy
56
+ self.frame = viewFrame
57
+ }
58
+ override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
59
+ super.touchesEnded(touches, withEvent: event)
60
+ if let rect = removeRect where CGRectIntersectsRect(self.frame, rect) {
61
+ self.removeFromSuperview()
62
+ }else{
12
- https://teratail.com/questions/32967
63
+ // add.hozon.frame.origin.y = 910
64
+ // add.hozon.frame.origin.y = 150
65
+ }
66
+
67
+ isMoveing = false
68
+
69
+ if position == self.frame.origin {
70
+ self.sendActionsForControlEvents(.TouchUpInside)}
71
+ }
72
+
73
+
13
74
  を使って作成しています。
14
75
 
15
76
  デリゲートではなくても