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

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

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

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

Q&A

解決済

1回答

523閲覧

swift UIViewに当たり判定を適用させる②

ataru2222

総合スコア272

Swift

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

0グッド

0クリップ

投稿2019/08/16 10:22

UIViewに当たり判定を適用させる
こちらのサイトを見ながら、テストを行っております。
記事が古いので修正を加えたり別の質問を行いコードを書き換えることで、Swift5での実機でテストをするところまではいけたのですが、
ボタンを押したところバグが出てしまい、その修正ができなかったので改めて質問させていただきました。

参考サイト

swift

1 2import UIKit 3 4class fourthViewController: UIViewController { 5 6 // UIDynamicAnimatorはインスタンスを保存しなければアニメーションが実行されない. 7 8 var animator : UIDynamicAnimator! 9 10 11 12 var gravity : UIGravityBehavior! 13 14 15 16 override func viewDidLoad() { 17 18 19 20 super.viewDidLoad() 21 22 23 24 self.view.backgroundColor = UIColor.cyan 25 26 27 28 // Labelを作成. 29 30 let myLabel: UILabel = UILabel(frame: CGRect(x: 0,y: 0,width:200,height:40)) 31 32 myLabel.backgroundColor = UIColor.orange 33 34 myLabel.layer.masksToBounds = true 35 36 myLabel.layer.cornerRadius = 20.0 37 38 myLabel.text = "Hello Swift!!" 39 40 myLabel.textColor = UIColor.white 41 42 myLabel.shadowColor = UIColor.gray 43 44 myLabel.textAlignment = NSTextAlignment.center 45 46 myLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: -200) 47 48 self.view.addSubview(myLabel) 49 50 51 52 // 障害物を作成. 53 54 let myBoxView = UIView(frame: CGRect(x:0,y: 0,width: 50,height: 40)) 55 56 myBoxView.backgroundColor = UIColor.blue 57 58 myBoxView.layer.masksToBounds = true 59 60 myBoxView.layer.position = CGPoint(x:self.view.frame.midX - 50,y:self.view.frame.midY) 61 62 self.view.addSubview(myBoxView) 63 64 65 66 // DropButtonを作成. 67 68 let myButton = UIButton(frame: CGRect(x:0,y: 0,width: 200,height: 50)) 69 70 myButton.layer.position = CGPoint(x: self.view.frame.midX,y: 500) 71 72 myButton.layer.masksToBounds = true 73 74 myButton.layer.cornerRadius = 20.0 75 76 myButton.setTitleColor(UIColor.white, for: UIControl.State.normal) 77 78 myButton.setTitleColor(UIColor.black, for: UIControl.State.highlighted) 79 80 myButton.backgroundColor = UIColor.red 81 82 myButton.setTitle("Dropped!", for: UIControl.State.normal) 83 84 myButton.addTarget(self, action: Selector(("onClickMyButton:")), for: UIControl.Event.touchUpInside) 85 86 myButton.tag = 0 87 88 self.view.addSubview(myButton) 89 90 91 92 // ResetButtonを作成. 93 94 let myResetButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 50)) 95 96 myResetButton.layer.position = CGPoint(x: self.view.frame.midX,y: 400) 97 98 myResetButton.layer.masksToBounds = true 99 100 myResetButton.layer.cornerRadius = 20.0 101 102 myResetButton.setTitleColor(UIColor.white, for: UIControl.State.normal) 103 104 myResetButton.setTitleColor(UIColor.black, for: UIControl.State.highlighted) 105 106 myResetButton.backgroundColor = UIColor.blue 107 108 myResetButton.setTitle("Reset!", for: UIControl.State.normal) 109 110 myResetButton.addTarget(self, action: Selector(("onClickMyButton:")), for: UIControl.Event.touchUpInside) 111 112 myResetButton.tag = 1 113 114 self.view.addSubview(myResetButton) 115 116 117 118 // UIDynamiAnimatorの生成とインスタンスの保存. 119 120 animator = UIDynamicAnimator(referenceView: self.view) 121 122 123 124 // 重力を作り、Viewに適用させる. 125 126 gravity = UIGravityBehavior(items: [myLabel]) 127 128 129 130 // Collisionを作成、Viewに適用させる. 131 132 let collision = UICollisionBehavior(items: [myLabel,myBoxView]) 133 134 135 136 // Collisionの挙動を指定. 137 138 collision.addBoundary(withIdentifier: "barrier" as NSString, for: UIBezierPath(rect: myBoxView.frame)) 139 //func addBoundary(withIdentifier identifier: NSCopying, for bezierPath: UIBezierPath) 140 141 142 // Collisionのアニメーションを実行. 143 144 animator.addBehavior(collision) 145 146 } 147 148 149 150 func onClickMyButton(sender : UIButton){ 151 152 153 154 switch(sender.tag) { 155 156 157 158 // DropButton. 159 160 case 0: 161 162 // Gravityのアニメーションを実行. 163 164 animator.addBehavior(gravity) 165 166 167 168 // ResetButton. 169 170 case 1: 171 172 173 174 let next = fourthViewController() 175 176 self.present(next, animated: false, completion: nil) 177 178 179 180 default: 181 182 print("error!") 183 184 } 185 186 } 187 188} 189

エラーコードは
2019-08-16 19:11:10.964556+0900 imageTap[12260:2683036] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[imageTap.fourthViewController onClickMyButton:]: unrecognized selector sent to instance 0x11fd16c40'

のようになっており訳は
キャッチされない例外 'NSInvalidArgumentException'によるアプリの終了、理由: '-[imageTap.fourthViewController onClickMyButton:]:認識されないセレクターがインスタンス0x11fd16c40に送信されました

この理由でクラッシュしているようです。
現状の自分の力では解決できないので、どなたかお力をお貸しください。

よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Swift

1myButton.addTarget(self, action: Selector(("onClickMyButton:")), for: UIControl.Event.touchUpInside) 2 3myResetButton.addTarget(self, action: Selector(("onClickMyButton:")), for: UIControl.Event.touchUpInside) 4 5func onClickMyButton(sender : UIButton){

上記のコードを、下記へ変更してください。(selectorと@objc)

Swift

1myButton.addTarget(self, action: #selector(onClickMyButton(sender:)), for: UIControl.Event.touchUpInside) 2 3myResetButton.addTarget(self, action: #selector(onClickMyButton(sender:)), for: UIControl.Event.touchUpInside) 4 5@objc func onClickMyButton(sender : UIButton){

投稿2019/08/16 11:06

dsuzuki

総合スコア1682

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

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

ataru2222

2019/08/16 22:16

できました。 旧コードからの変更点であるselecterと@objcの変更点についても学ぶことができました。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問