Swift3 カスタムボタンを使ってゴミ箱を作りたい
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 956
Swift3でiOSアプリを作っています。
ボタンを生成して動かす事は出来ました。
ボタンをゴミ箱まで移動させて、そのボタンを削除したいと思います。
そこで移動中のカスタムセルの座標を知って、ゴミ箱の座標の中に入ったら削除というプロセスを
実行しようとうしています。
今作っているコードは以下のようになります。
import UIKit
class gomiViewController: UIViewController {
var viewCount: CGFloat = 0
let button1 = gomiCustomButton(type: .custom)
let button2 = UIButton()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// ボタン1を生成
button1.frame = CGRect(x:100,y:100,width:100,height:50)
button1.setTitle("MOVE", for: .normal)
button1.setTitleColor(UIColor.blue, for: .normal)
button1.backgroundColor = UIColor.yellow
button1.isUserInteractionEnabled = true
button1.tag = 1
view.addSubview(button1)
// ボタン2を生成
button2.frame = CGRect(x:100,y:400,width:100,height:50)
button2.setTitle("ゴミ箱", for: .normal)
button2.setTitleColor(UIColor.blue, for: .normal)
button2.backgroundColor = UIColor.green
button2.isUserInteractionEnabled = true
button2.tag = 2
view.addSubview(button2)
//Labelを生成
label.frame = CGRect(x:250,y:100,width:100,height:50)
label.backgroundColor = UIColor.orange
label.textColor = UIColor.white
label.text = "x: y: "
label.shadowColor = UIColor.gray
label.textAlignment = NSTextAlignment.center
self.view.addSubview(label)
}
}
// CustomButton Class
class gomiCustomButton: UIButton {
var isMoveing: Bool = false
var position: CGPoint!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
position = self.frame.origin
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
isMoveing = true
let touchEvent = touches.first!
// ドラッグ前の座標
let preDx = touchEvent.previousLocation(in: superview).x
let preDy = touchEvent.previousLocation(in: superview).y
// ドラッグ後の座標
let newDx = touchEvent.location(in: superview).x
let newDy = touchEvent.location(in: superview).y
// ドラッグしたx座標の移動距離
let dx = newDx - preDx
// ドラッグしたy座標の移動距離
let dy = newDy - preDy
// 画像のフレーム
var viewFrame: CGRect = self.frame
// 移動分を反映させる
viewFrame.origin.x += dx
viewFrame.origin.y += dy
self.frame = viewFrame
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
isMoveing = false
if position == self.frame.origin {
self.sendActions(for: .touchUpInside)
}
}
}
前段階として、Labelに座標を表示させようと思っています。
ここで、座標の取り出し方が分からず困ってます
移動中のカスタムセルからの座標の取り出し方はどうしたらいいのでしょうか?
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
centerプロパティで親ビューからの中心座標(x,y)がとれます。
import UIKit
let label = UILabel() // ★★★ スコープ拡大
class gomiViewController: UIViewController {
var viewCount: CGFloat = 0
let button1 = gomiCustomButton(type: .custom)
let button2 = UIButton()
//let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// ボタン1を生成
button1.frame = CGRect(x:100,y:100,width:100,height:50)
button1.setTitle("MOVE", for: .normal)
button1.setTitleColor(UIColor.blue, for: .normal)
button1.backgroundColor = UIColor.yellow
button1.isUserInteractionEnabled = true
button1.tag = 1
view.addSubview(button1)
// ボタン2を生成
button2.frame = CGRect(x:100,y:400,width:100,height:50)
button2.setTitle("ゴミ箱", for: .normal)
button2.setTitleColor(UIColor.blue, for: .normal)
button2.backgroundColor = UIColor.green
button2.isUserInteractionEnabled = true
button2.tag = 2
view.addSubview(button2)
//Labelを生成
label.frame = CGRect(x:250,y:100,width:150,height:50) // ★★★ widthをちょっと広げました
label.backgroundColor = UIColor.orange
label.textColor = UIColor.white
label.text = "x: y: "
label.shadowColor = UIColor.gray
label.textAlignment = NSTextAlignment.center
self.view.addSubview(label)
}
}
// CustomButton Class
class gomiCustomButton: UIButton {
var isMoveing: Bool = false
var position: CGPoint!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
position = self.frame.origin
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
isMoveing = true
let touchEvent = touches.first!
// ドラッグ前の座標
let preDx = touchEvent.previousLocation(in: superview).x
let preDy = touchEvent.previousLocation(in: superview).y
// ドラッグ後の座標
let newDx = touchEvent.location(in: superview).x
let newDy = touchEvent.location(in: superview).y
// ドラッグしたx座標の移動距離
let dx = newDx - preDx
// ドラッグしたy座標の移動距離
let dy = newDy - preDy
// 画像のフレーム
var viewFrame: CGRect = self.frame
// 移動分を反映させる
viewFrame.origin.x += dx
viewFrame.origin.y += dy
self.frame = viewFrame
// ★★★ 座標を表示
label.text = String(describing: "x:\(floor(self.center.x)) y:\(floor(self.center.y))")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
isMoveing = false
if position == self.frame.origin {
self.sendActions(for: .touchUpInside)
}
}
}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
2016/11/15 10:18
無事動作確認ができました。
リアルタイムで座標を取得する事ができました