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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

2回答

2126閲覧

Swiftのカスタムボタンついてお願いします。

A-B-fly_man

総合スコア27

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/04/21 08:20

わかりやすいのでこれ使うことにします。

###前提・実現したいこと
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)を普通に格納してみようとしましたができませんでした。できるならばお願い致します。

ということなのですがわかる方はご教授お願い致します。

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

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

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

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

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

guest

回答2

0

ベストアンサー

Stripeさんのアドバイスの通り、UIButtonのサブクラスの中で移動処理を行う様に書きなおしてみました。
お試しください。

こちらをベースにしたほうが使い回しがきくと思います。

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 let button1 = CustomButton(type: .Custom) 6 let button2 = CustomButton(type: .Custom) 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 // ボタン1を生成 12 button1.frame = CGRectMake(100, 100, 200, 50) 13 button1.setTitle("Button1", forState: .Normal) 14 button1.setTitleColor(UIColor.blueColor(), forState: .Normal) 15 button1.backgroundColor = UIColor.yellowColor() 16 button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside) 17 button1.userInteractionEnabled = true 18 button1.tag = 1 19 view.addSubview(button1) 20 21 // ボタン2を生成 22 button2.frame = CGRectMake(100, 200, 200, 50) 23 button2.setTitle("Button2", forState: .Normal) 24 button2.setTitleColor(UIColor.blueColor(), forState: .Normal) 25 button2.backgroundColor = UIColor.greenColor() 26 button2.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside) 27 button2.userInteractionEnabled = true 28 button2.tag = 2 29 view.addSubview(button2) 30 } 31 32 func buttonPressed(sender: UIButton) { 33 if !button1.isMoveing && sender.tag == 1 { 34 // ボタン1押下時のイベント 35 print("Push! Button1") 36 } 37 38 if !button2.isMoveing && sender.tag == 2 { 39 // ボタン2押下時のイベント 40 print("Push! Button2") 41 } 42 } 43} 44 45 46// CustomButton Class 47class CustomButton: UIButton { 48 49 var isMoveing: Bool = false 50 var position: CGPoint! 51 52 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 53 super.touchesBegan(touches, withEvent: event) 54 position = self.frame.origin 55 } 56 57 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 58 super.touchesMoved(touches, withEvent: event) 59 60 isMoveing = true 61 62 let touchEvent = touches.first! 63 64 // ドラッグ前の座標 65 let preDx = touchEvent.previousLocationInView(superview).x 66 let preDy = touchEvent.previousLocationInView(superview).y 67 68 // ドラッグ後の座標 69 let newDx = touchEvent.locationInView(superview).x 70 let newDy = touchEvent.locationInView(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 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 88 super.touchesEnded(touches, withEvent: event) 89 isMoveing = false 90 if position == self.frame.origin { 91 self.sendActionsForControlEvents(.TouchUpInside) 92 } 93 } 94}

投稿2016/04/21 13:53

_Kentarou

総合スコア8490

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

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

A-B-fly_man

2016/04/22 07:27

毎回ありがとうございます。助かります! 試してみたところUIButton自体を増やす方法も簡単だったためわかりやすかったです。 何日も困っていたのですごく助かりました。本当にありがとうございました!
guest

0

ViewController内のtouchesMoved()で、touchEvent1 touchEvent2 touchEvent3は全く同じ値です。
だから、tag1 tag2 tag3も全く同じ値になります。
そのため、if文の中のmoveView2(touchEvent2, vi2: TAO02)moveView3(touchEvent3, vi3: TAO03)は絶対に実行されません。

そもそも、UIButtonをサブクラス化しているなら、ボタンの移動処理をその中で完結できます。ViewControllerに処理を委ねる必要はありません。

投稿2016/04/21 09:46

Stripe

総合スコア2183

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

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

A-B-fly_man

2016/04/22 07:25

コメントありがとうございます。 助かりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問