現在、タップとスワイプの二つを一つのボタンで行おうとしています。
ボタンをタップしたらUIViewを出して
ボタンをスワイプしたらそのボタンが指に沿って移動する・・・
そこで僕のボタンのスワイプの方法は現在の指と移動させるボタンの
座標を取得してそのボタンに指が入っていたら、ボタンを移動させる
・・・とそんな感じです。
しかし、ボタンの上は座標取得できなくて動かすことができません。
根本的なやり方が違うのか、ボタンの動かし方がおかしいのか、
とても迷っています。
僕よりも知識のある皆様はどのような考え方で行うことがベストでしょうか?
自分だったらどういう考え方でコードを書いて行くか・・・など
初心者なので出来る限りわかりやすくお願い致します。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/03 22:38 編集
2016/04/06 04:22
回答2件
0
なるほど!参考になりました。
助かりました〜( ^ ^ )
投稿2016/04/06 06:54
総合スコア42
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
ベストアンサー
ボタンをタップしたらUIViewを出して
ボタンをスワイプしたらそのボタンが指に沿って移動する・・・
要件を満たしているか分かりませんが、ここからアレンジすればやりたい事ができるようになると思います。
下記のコードをViewControllerにそのまま貼り付けると動くと思います。
ボタン1とボタン2がありそれぞれ押すと左に黄色、緑色のViewがaddされます、そしてUIButtonはドラッグで移動できます。
swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 var viewCount: CGFloat = 0 6 7 let button1 = CustomButton(type: .Custom) 8 let button2 = CustomButton(type: .Custom) 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 // ボタン1を生成 14 button1.frame = CGRectMake(100, 100, 200, 50) 15 button1.setTitle("Button1", forState: .Normal) 16 button1.setTitleColor(UIColor.blueColor(), forState: .Normal) 17 button1.backgroundColor = UIColor.yellowColor() 18 button1.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside) 19 button1.userInteractionEnabled = true 20 button1.tag = 1 21 view.addSubview(button1) 22 23 // ボタン2を生成 24 button2.frame = CGRectMake(100, 200, 200, 50) 25 button2.setTitle("Button2", forState: .Normal) 26 button2.setTitleColor(UIColor.blueColor(), forState: .Normal) 27 button2.backgroundColor = UIColor.greenColor() 28 button2.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside) 29 button2.userInteractionEnabled = true 30 button2.tag = 2 31 view.addSubview(button2) 32 } 33 34 func buttonPressed(sender: UIButton) { 35 if !button1.isMoveing && sender.tag == 1 { 36 // ボタン1押下時のイベント 37 generateLabel("1", color: UIColor.yellowColor()) 38 } 39 40 if !button2.isMoveing && sender.tag == 2 { 41 // ボタン2押下時のイベント 42 generateLabel("2", color: UIColor.greenColor()) 43 } 44 } 45 46 func generateLabel(text: String, color: UIColor) { 47 let v = UILabel(frame: CGRectMake(10, 10 + viewCount * 40 , 30, 30)) 48 v.text = text 49 v.textAlignment = .Center 50 v.backgroundColor = color 51 view.addSubview(v) 52 viewCount += 1 53 } 54} 55 56 57// CustomButton Class 58class CustomButton: UIButton { 59 60 var isMoveing: Bool = false 61 var position: CGPoint! 62 63 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 64 super.touchesBegan(touches, withEvent: event) 65 position = self.frame.origin 66 } 67 68 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 69 super.touchesMoved(touches, withEvent: event) 70 71 isMoveing = true 72 73 let touchEvent = touches.first! 74 75 // ドラッグ前の座標 76 let preDx = touchEvent.previousLocationInView(superview).x 77 let preDy = touchEvent.previousLocationInView(superview).y 78 79 // ドラッグ後の座標 80 let newDx = touchEvent.locationInView(superview).x 81 let newDy = touchEvent.locationInView(superview).y 82 83 // ドラッグしたx座標の移動距離 84 let dx = newDx - preDx 85 86 // ドラッグしたy座標の移動距離 87 let dy = newDy - preDy 88 89 // 画像のフレーム 90 var viewFrame: CGRect = self.frame 91 92 // 移動分を反映させる 93 viewFrame.origin.x += dx 94 viewFrame.origin.y += dy 95 self.frame = viewFrame 96 } 97 98 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 99 super.touchesEnded(touches, withEvent: event) 100 isMoveing = false 101 if position == self.frame.origin { 102 self.sendActionsForControlEvents(.TouchUpInside) 103 } 104 } 105}
投稿2016/04/03 02:53
編集2016/04/21 16:03総合スコア8490
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/06 05:26
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。