わかりやすいのでこれ使うことにします。
###前提・実現したいこと
Swiftの座標取得ができないです...から作ってみたものが
① UIViewController上に移動できるボタンを作る。
② ①を3回繰り返し3個の移動できるボタンを作る。(作ったボタンを仮に A B C とします。)
③ A はドラッグできますが、 B C をドラッグしようしても何故か A が反応し、そのまま移動する。( B 、 C は無反応です。)
なので、 A B C がそれぞれ動くようにしたいです。
###該当のソースコード
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches { // 座標取得 var point = touch.locationInView(self.view) print(point) } // タッチイベントを取得 let touchEvent1 = touches.first! let touchEvent2 = touches.first! let touchEvent3 = touches.first! let tag1 = touchEvent1.view?.tag let tag2 = touchEvent2.view?.tag let tag3 = touchEvent3.view?.tag if tag1 == 999 {moveView1(touchEvent1, vi1: TAO01) }else if tag2 == 999{moveView2(touchEvent2, vi2: TAO02) }else if tag3 == 999{moveView3(touchEvent3, vi3: TAO03)} }
func moveView1<T1: UIView>(touchEvent1: UITouch, vi1: T1) {
// ドラッグ前の座標
let preDx1 = touchEvent1.previousLocationInView(self.view).x
let preDy1 = touchEvent1.previousLocationInView(self.view).y
// ドラッグ後の座標 let newDx1 = touchEvent1.locationInView(self.view).x let newDy1 = touchEvent1.locationInView(self.view).y // ドラッグしたx座標の移動距離 let dx1 = newDx1 - preDx1 // ドラッグしたy座標の移動距離 let dy1 = newDy1 - preDy1 // 画像のフレーム var viewFrame1: CGRect = vi1.frame // 移動分を反映させる viewFrame1.origin.x += dx1 viewFrame1.origin.y += dy1 vi1.frame = viewFrame1 } func moveView2<T2: UIView>(touchEvent2: UITouch, vi2: T2) { // ドラッグ前の座標 let preDx2 = touchEvent2.previousLocationInView(self.view).x let preDy2 = touchEvent2.previousLocationInView(self.view).y // ドラッグ後の座標 let newDx2 = touchEvent2.locationInView(self.view).x let newDy2 = touchEvent2.locationInView(self.view).y // ドラッグしたx座標の移動距離 let dx2 = newDx2 - preDx2 // ドラッグしたy座標の移動距離 let dy2 = newDy2 - preDy2 // 画像のフレーム var viewFrame2: CGRect = vi2.frame // 移動分を反映させる viewFrame2.origin.x += dx2 viewFrame2.origin.y += dy2 vi2.frame = viewFrame2 } func moveView3<T3: UIView>(touchEvent3: UITouch, vi3: T3) { // ドラッグ前の座標 let preDx3 = touchEvent3.previousLocationInView(self.view).x let preDy3 = touchEvent3.previousLocationInView(self.view).y // ドラッグ後の座標 let newDx3 = touchEvent3.locationInView(self.view).x let newDy3 = touchEvent3.locationInView(self.view).y // ドラッグしたx座標の移動距離 let dx3 = newDx3 - preDx3 // ドラッグしたy座標の移動距離 let dy3 = newDy3 - preDy3 // 画像のフレーム var viewFrame3: CGRect = vi3.frame // 移動分を反映させる viewFrame3.origin.x += dx3 viewFrame3.origin.y += dy3 vi3.frame = viewFrame3 } class CustomButton1: UIButton { var vc1: UIViewController! var isMoveing1: Bool = false override func touchesMoved(touches1: Set<UITouch>, withEvent event1: UIEvent?) { super.touchesMoved(touches1, withEvent: event1) isMoveing1 = true vc1.touchesMoved(touches1, withEvent: event1)} override func touchesEnded(touches1: Set<UITouch>, withEvent event1: UIEvent?) { super.touchesEnded(touches1, withEvent: event1) self.superview!.touchesEnded(touches1, withEvent: event1) super.touchesEnded(touches1, withEvent: event1) isMoveing1 = false vc1.touchesEnded(touches1, withEvent: event1) }} class CustomButton2: UIButton { var vc2: UIViewController! var isMoveing2: Bool = false override func touchesMoved(touches1: Set<UITouch>, withEvent event1: UIEvent?) { super.touchesMoved(touches1, withEvent: event1) isMoveing2 = true vc2.touchesMoved(touches1, withEvent: event1)} override func touchesEnded(touches2: Set<UITouch>, withEvent event2: UIEvent?) { super.touchesEnded(touches2, withEvent: event2) self.superview!.touchesEnded(touches2, withEvent: event2) super.touchesEnded(touches2, withEvent: event2) isMoveing2 = false vc2.touchesEnded(touches2, withEvent: event2) }} class CustomButton3: UIButton { var vc3: UIViewController! var isMoveing3: Bool = false override func touchesMoved(touches3: Set<UITouch>, withEvent event3: UIEvent?) { super.touchesMoved(touches3, withEvent: event3) isMoveing3 = true vc3.touchesMoved(touches3, withEvent: event3)} override func touchesEnded(touches3: Set<UITouch>, withEvent event3: UIEvent?) { super.touchesEnded(touches3, withEvent: event3) self.superview!.touchesEnded(touches3, withEvent: event3) super.touchesEnded(touches3, withEvent: event3) isMoveing3 = false vc3.touchesEnded(touches3, withEvent: event3) }}
TAO1、TAO2、TAO3を作るコードは⬆︎が記述されているコードより下の方にあります。コピペなので省きます。
###試したこと
作る分だけ関数を増やしてみたり、単数個しかないものはとりあえず複数個作ってみました。それでも結局 A しか動きませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
できれば配列に格納できればいいのですが、var TAO01 = CustomButton1(type: .Custom)を普通に格納してみようとしましたができませんでした。できるならばお願い致します。
ということなのですがわかる方はご教授お願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/22 07:27