前回載せたコードが汎用性がなかったため書き直しました。
以下のコードで消したい場所のCGRectを指定するとドラッグを離した時、その場所だったらオブジェクトが消えます。
View同士が重なっているかどうかを判定する等は以下のサイトが参考になります。
CGGeometryクラスの位置やサイズ操作系の関数紹介
swift
1import UIKit
2
3class ViewController: UIViewController {
4
5 let button1 = CustomButton(type: .Custom)
6 let button2 = CustomButton(type: .Custom)
7
8 let removeZoneView = UIView(frame: CGRectMake(0, 0, 200, 200))
9
10 override func viewDidLoad() {
11 super.viewDidLoad()
12
13 removeZoneView.backgroundColor = UIColor.redColor()
14 view.addSubview(removeZoneView)
15
16 // ボタン1を生成
17 button1.frame = CGRectMake(100, 300, 200, 50)
18 button1.setTitle("Button1", forState: .Normal)
19 button1.setTitleColor(UIColor.blueColor(), forState: .Normal)
20 button1.backgroundColor = UIColor.yellowColor()
21 button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
22 button1.userInteractionEnabled = true
23 button1.tag = 1
24 // 消したい場所(frame)を設定
25 button1.removeRect = removeZoneView.frame
26 view.addSubview(button1)
27
28 // ボタン2を生成
29 button2.frame = CGRectMake(100, 400, 200, 50)
30 button2.setTitle("Button2", forState: .Normal)
31 button2.setTitleColor(UIColor.blueColor(), forState: .Normal)
32 button2.backgroundColor = UIColor.greenColor()
33 button2.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
34 button2.userInteractionEnabled = true
35 button2.tag = 2
36 button2.removeRect = removeZoneView.frame
37 view.addSubview(button2)
38 }
39
40 func buttonPressed(sender: UIButton) {
41 if !button1.isMoveing && sender.tag == 1 {
42 // ボタン1押下時のイベント
43 print("Push! Button1")
44 }
45
46 if !button2.isMoveing && sender.tag == 2 {
47 // ボタン2押下時のイベント
48 print("Push! Button2")
49 }
50 }
51}
52
53
54// CustomButton Class
55class CustomButton: UIButton {
56
57 var isMoveing : Bool = false
58 var removeRect: CGRect!
59 var position : CGPoint!
60
61 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
62 super.touchesBegan(touches, withEvent: event)
63 position = self.frame.origin
64 }
65
66 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
67 super.touchesMoved(touches, withEvent: event)
68
69 isMoveing = true
70
71 let touchEvent = touches.first!
72
73 // ドラッグ前の座標
74 let preDx = touchEvent.previousLocationInView(superview).x
75 let preDy = touchEvent.previousLocationInView(superview).y
76
77 // ドラッグ後の座標
78 let newDx = touchEvent.locationInView(superview).x
79 let newDy = touchEvent.locationInView(superview).y
80
81 // ドラッグしたx座標の移動距離
82 let dx = newDx - preDx
83
84 // ドラッグしたy座標の移動距離
85 let dy = newDy - preDy
86
87 // 画像のフレーム
88 var viewFrame: CGRect = self.frame
89
90 // 移動分を反映させる
91 viewFrame.origin.x += dx
92 viewFrame.origin.y += dy
93 self.frame = viewFrame
94 }
95
96 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
97 super.touchesEnded(touches, withEvent: event)
98
99 if let rect = removeRect where CGRectIntersectsRect(self.frame, rect) {
100 // 消したいFrameと重なっている場合
101 self.removeFromSuperview()
102 }
103
104 isMoveing = false
105 if position == self.frame.origin {
106 self.sendActionsForControlEvents(.TouchUpInside)
107 }
108 }
109}
サンプルはボタンが2つ表示されますが、赤いViewの上に少しでも重なったらボタンが消えます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/22 07:27