Swift3でiOSアプリを作っています。
ボタンを生成して動かす事は出来ました。
ボタンをゴミ箱まで移動させて、そのボタンを削除したいと思います。
そこで移動中のカスタムセルの座標を知って、ゴミ箱の座標の中に入ったら削除というプロセスを
実行しようとうしています。
今作っているコードは以下のようになります。
swift
1import UIKit 2 3class gomiViewController: UIViewController { 4 5 var viewCount: CGFloat = 0 6 7 let button1 = gomiCustomButton(type: .custom) 8 let button2 = UIButton() 9 let label = UILabel() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 // ボタン1を生成 15 button1.frame = CGRect(x:100,y:100,width:100,height:50) 16 button1.setTitle("MOVE", for: .normal) 17 button1.setTitleColor(UIColor.blue, for: .normal) 18 button1.backgroundColor = UIColor.yellow 19 button1.isUserInteractionEnabled = true 20 button1.tag = 1 21 view.addSubview(button1) 22 23 // ボタン2を生成 24 button2.frame = CGRect(x:100,y:400,width:100,height:50) 25 button2.setTitle("ゴミ箱", for: .normal) 26 button2.setTitleColor(UIColor.blue, for: .normal) 27 button2.backgroundColor = UIColor.green 28 button2.isUserInteractionEnabled = true 29 button2.tag = 2 30 view.addSubview(button2) 31 32 //Labelを生成 33 label.frame = CGRect(x:250,y:100,width:100,height:50) 34 label.backgroundColor = UIColor.orange 35 label.textColor = UIColor.white 36 label.text = "x: y: " 37 label.shadowColor = UIColor.gray 38 label.textAlignment = NSTextAlignment.center 39 self.view.addSubview(label) 40 41 } 42} 43 44// CustomButton Class 45class gomiCustomButton: UIButton { 46 47 var isMoveing: Bool = false 48 var position: CGPoint! 49 50 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 51 super.touchesBegan(touches, with: event) 52 position = self.frame.origin 53 } 54 55 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 56 super.touchesMoved(touches, with: event) 57 58 isMoveing = true 59 60 let touchEvent = touches.first! 61 62 // ドラッグ前の座標 63 let preDx = touchEvent.previousLocation(in: superview).x 64 let preDy = touchEvent.previousLocation(in: superview).y 65 66 // ドラッグ後の座標 67 let newDx = touchEvent.location(in: superview).x 68 let newDy = touchEvent.location(in: superview).y 69 70 // ドラッグしたx座標の移動距離 71 let dx = newDx - preDx 72 73 // ドラッグしたy座標の移動距離 74 let dy = newDy - preDy 75 76 // 画像のフレーム 77 var viewFrame: CGRect = self.frame 78 79 // 移動分を反映させる 80 viewFrame.origin.x += dx 81 viewFrame.origin.y += dy 82 self.frame = viewFrame 83 } 84 85 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 86 super.touchesEnded(touches, with: event) 87 isMoveing = false 88 if position == self.frame.origin { 89 self.sendActions(for: .touchUpInside) 90 } 91 } 92} 93
前段階として、Labelに座標を表示させようと思っています。
ここで、座標の取り出し方が分からず困ってます
移動中のカスタムセルからの座標の取り出し方はどうしたらいいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/15 01:18