iOSのアプリ開発を始めました。画面の中心にラベルを配置し、それをタッチしてドラッグするとラベルが移動するプログラムを作成しようとしています。しかし実行すると、ラベルの表示まではうまくいくのですが、タッチしてドラッグしてもラベルが移動しません。デバッグしたところ、タッチを感知した時に呼ばれるメソッド、ドラッグした時に呼ばれるメソッド、指が離れる時に呼ばれるメソッドの3つが呼び出されていないことがわかりました。色々試行錯誤したのですが、解決策が出てこないので、どなたかご指摘よろしくお願いいたします。
import UIKit class ViewController: UIViewController { var label:UILabel! var myLabel: UILabel! var turn = 1 //初期値1の変数turn var stage:[[Int]] = [ [0,0,0,0,0,0,0,0], //コマのデータを格納するための変数 [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0] ] override func viewDidLoad() {//親クラスを編集することなく、子クラスの中で親クラスのメソッドを上書きできる super.viewDidLoad() // Labelを生成. myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 80, height: 80)) myLabel.text = "Drag!" myLabel.textAlignment = NSTextAlignment.center myLabel.backgroundColor = UIColor.red myLabel.layer.masksToBounds = true myLabel.center = self.view.center myLabel.layer.cornerRadius = 40.0 // Labelをviewに追加. self.view.addSubview(myLabel) print("駒がViewに追加されました") // Do any additional setup after loading the view, typically from a nib. let (screenWidth, screenHeight) = (self.view.bounds.width, self.view.bounds.height) let (width, height) = (screenWidth/8, screenHeight/20) //1マスあたりの幅と高さを指定 for i in 0 ..< 8{ for j in 0 ..< 20{ //jが縦、iが横(ただし、デバイスを立てで見たとき) let button = UIButton(frame: CGRectMake(width*CGFloat(i),height*CGFloat(j+1),width,height)) button.setTitle(" ", for: .normal) //コメントアウトしても実行結果は変わらない、、 button.setTitleColor(UIColor.blue/*black*/, for: .normal) //盤上の駒の色を指定するプロパティ button.setTitleColor(UIColor.yellow/*lightGray*/, for: .highlighted) //いつ発動するの? button.layer.borderColor = UIColor.black/*black*/.cgColor //線の色を指定するプロパティ button.layer.borderWidth = 0.5 //線幅を指定するプロパティ self.view.addSubview(button)//コメントアウトするとマス目がなくなったから必要っぽい } } self.label = UILabel(frame: CGRectMake(0,0,screenWidth,height)) self.view.addSubview(self.label!) print("マス目がViewに追加されました") } func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect{ return CGRect(x:x,y:y,width:width,height:height) } /* タッチを感知した際に呼ばれるメソッド. */ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { print("touchesBeganが呼ばれました") // Labelアニメーション. UIView.animate(withDuration: 0.06, // アニメーション中の処理. animations: { () -> Void in // 縮小用アフィン行列を作成する. self.myLabel.transform = CGAffineTransform(scaleX: 0.9, y: 0.9) }) { (Bool) -> Void in } } /* ドラッグを感知した際に呼ばれるメソッド. (ドラッグ中何度も呼ばれる) */ override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { print("タッチが感知されました") // タッチイベントを取得. let aTouch: UITouch = touches.first! // 移動した先の座標を取得. let location = aTouch.location(in: self.view) // 移動する前の座標を取得. let prevLocation = aTouch.previousLocation(in: self.view) // CGRect生成. var myFrame: CGRect = self.view.frame // ドラッグで移動したx, y距離をとる. let deltaX: CGFloat = location.x - prevLocation.x let deltaY: CGFloat = location.y - prevLocation.y // 移動した分の距離をmyFrameの座標にプラスする. myFrame.origin.x += deltaX myFrame.origin.y += deltaY // frameにmyFrameを追加. self.view.frame = myFrame } /* 指が離れたことを感知した際に呼ばれるメソッド. */ override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { print("指が離れました") // Labelアニメーション. UIView.animate(withDuration: 0.1, // アニメーション中の処理. animations: { () -> Void in // 拡大用アフィン行列を作成する. self.myLabel.transform = CGAffineTransform(scaleX: 0.4, y: 0.4) // 縮小用アフィン行列を作成する. self.myLabel.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) }) { (Bool) -> Void in } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/13 09:37