質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2411閲覧

Swift3 カスタムボタンを使ってゴミ箱を作りたい

Robokun

総合スコア53

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

1グッド

0クリップ

投稿2016/11/11 22:38

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に座標を表示させようと思っています。
ここで、座標の取り出し方が分からず困ってます
移動中のカスタムセルからの座標の取り出し方はどうしたらいいのでしょうか?

kbel23👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

centerプロパティで親ビューからの中心座標(x,y)がとれます。

swift

1import UIKit 2 3let label = UILabel() // ★★★ スコープ拡大 4 5class gomiViewController: UIViewController { 6 7 var viewCount: CGFloat = 0 8 9 let button1 = gomiCustomButton(type: .custom) 10 let button2 = UIButton() 11 //let label = UILabel() 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 // ボタン1を生成 17 button1.frame = CGRect(x:100,y:100,width:100,height:50) 18 button1.setTitle("MOVE", for: .normal) 19 button1.setTitleColor(UIColor.blue, for: .normal) 20 button1.backgroundColor = UIColor.yellow 21 button1.isUserInteractionEnabled = true 22 button1.tag = 1 23 view.addSubview(button1) 24 25 // ボタン2を生成 26 button2.frame = CGRect(x:100,y:400,width:100,height:50) 27 button2.setTitle("ゴミ箱", for: .normal) 28 button2.setTitleColor(UIColor.blue, for: .normal) 29 button2.backgroundColor = UIColor.green 30 button2.isUserInteractionEnabled = true 31 button2.tag = 2 32 view.addSubview(button2) 33 34 //Labelを生成 35 label.frame = CGRect(x:250,y:100,width:150,height:50) // ★★★ widthをちょっと広げました 36 label.backgroundColor = UIColor.orange 37 label.textColor = UIColor.white 38 label.text = "x: y: " 39 label.shadowColor = UIColor.gray 40 label.textAlignment = NSTextAlignment.center 41 self.view.addSubview(label) 42 43 } 44} 45 46// CustomButton Class 47class gomiCustomButton: UIButton { 48 49 var isMoveing: Bool = false 50 var position: CGPoint! 51 52 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 53 super.touchesBegan(touches, with: event) 54 position = self.frame.origin 55 } 56 57 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 58 super.touchesMoved(touches, with: event) 59 60 isMoveing = true 61 62 let touchEvent = touches.first! 63 64 // ドラッグ前の座標 65 let preDx = touchEvent.previousLocation(in: superview).x 66 let preDy = touchEvent.previousLocation(in: superview).y 67 68 // ドラッグ後の座標 69 let newDx = touchEvent.location(in: superview).x 70 let newDy = touchEvent.location(in: superview).y 71 72 // ドラッグしたx座標の移動距離 73 let dx = newDx - preDx 74 75 // ドラッグしたy座標の移動距離 76 let dy = newDy - preDy 77 78 // 画像のフレーム 79 var viewFrame: CGRect = self.frame 80 81 // 移動分を反映させる 82 viewFrame.origin.x += dx 83 viewFrame.origin.y += dy 84 self.frame = viewFrame 85 86 // ★★★ 座標を表示 87 label.text = String(describing: "x:\(floor(self.center.x)) y:\(floor(self.center.y))") 88 } 89 90 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 91 super.touchesEnded(touches, with: event) 92 93 isMoveing = false 94 if position == self.frame.origin { 95 self.sendActions(for: .touchUpInside) 96 } 97 } 98} 99

投稿2016/11/12 20:56

fromageblanc

総合スコア2724

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Robokun

2016/11/15 01:18

毎回ありがとうございます。 無事動作確認ができました。 リアルタイムで座標を取得する事ができました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問