こちらのサイトを見ながら
UIViewに当たり判定を適用させる
当たり判定の勉強をしようとしているのですが、
少し前の記事なのでエラーまるけになっていました。
自力で修正できるところはしたのですが、
1箇所だけ直せなかったので質問させていただきました。
swift
1import UIKit 2 3class fourthViewController: UIViewController { 4 5 // UIDynamicAnimatorはインスタンスを保存しなければアニメーションが実行されない. 6 7 var animator : UIDynamicAnimator! 8 9 10 11 var gravity : UIGravityBehavior! 12 13 14 15 override func viewDidLoad() { 16 17 18 19 super.viewDidLoad() 20 21 22 23 self.view.backgroundColor = UIColor.cyan 24 25 26 27 // Labelを作成. 28 29 let myLabel: UILabel = UILabel(frame: CGRect(x: 0,y: 0,width:200,height:40)) 30 31 myLabel.backgroundColor = UIColor.orange 32 33 myLabel.layer.masksToBounds = true 34 35 myLabel.layer.cornerRadius = 20.0 36 37 myLabel.text = "Hello Swift!!" 38 39 myLabel.textColor = UIColor.white 40 41 myLabel.shadowColor = UIColor.gray 42 43 myLabel.textAlignment = NSTextAlignment.center 44 45 myLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: -200) 46 47 self.view.addSubview(myLabel) 48 49 50 51 // 障害物を作成. 52 53 let myBoxView = UIView(frame: CGRect(x:0,y: 0,width: 50,height: 40)) 54 55 myBoxView.backgroundColor = UIColor.blue 56 57 myBoxView.layer.masksToBounds = true 58 59 myBoxView.layer.position = CGPoint(x:self.view.frame.midX - 50,y:self.view.frame.midY) 60 61 self.view.addSubview(myBoxView) 62 63 64 65 // DropButtonを作成. 66 67 let myButton = UIButton(frame: CGRect(x:0,y: 0,width: 200,height: 50)) 68 69 myButton.layer.position = CGPoint(x: self.view.frame.midX,y: 500) 70 71 myButton.layer.masksToBounds = true 72 73 myButton.layer.cornerRadius = 20.0 74 75 myButton.setTitleColor(UIColor.white, for: UIControl.State.normal) 76 77 myButton.setTitleColor(UIColor.black, for: UIControl.State.highlighted) 78 79 myButton.backgroundColor = UIColor.red 80 81 myButton.setTitle("Dropped!", for: UIControl.State.normal) 82 83 myButton.addTarget(self, action: Selector("onClickMyButton:"), for: UIControl.Event.touchUpInside) 84 85 myButton.tag = 0 86 87 self.view.addSubview(myButton) 88 89 90 91 // ResetButtonを作成. 92 93 let myResetButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 50)) 94 95 myResetButton.layer.position = CGPoint(x: self.view.frame.midX,y: 400) 96 97 myResetButton.layer.masksToBounds = true 98 99 myResetButton.layer.cornerRadius = 20.0 100 101 myResetButton.setTitleColor(UIColor.white, for: UIControl.State.normal) 102 103 myResetButton.setTitleColor(UIColor.black, for: UIControl.State.highlighted) 104 105 myResetButton.backgroundColor = UIColor.blue 106 107 myResetButton.setTitle("Reset!", for: UIControl.State.normal) 108 109 myResetButton.addTarget(self, action: Selector(("onClickMyButton:")), for: UIControl.Event.touchUpInside) 110 111 myResetButton.tag = 1 112 113 self.view.addSubview(myResetButton) 114 115 116 117 // UIDynamiAnimatorの生成とインスタンスの保存. 118 119 animator = UIDynamicAnimator(referenceView: self.view) 120 121 122 123 // 重力を作り、Viewに適用させる. 124 125 gravity = UIGravityBehavior(items: [myLabel]) 126 127 128 129 // Collisionを作成、Viewに適用させる. 130 131 let collision = UICollisionBehavior(items: [myLabel,myBoxView]) 132 133 134 135 // ★★★★★Collisionの挙動を指定. 136 //ここでエラー Ambiguous use of 'addBoundaryWithIdentifier' 137 collision.addBoundaryWithIdentifier("barrier", forPath: UIBezierPath(rect: myBoxView.frame)) 138 139 140 141 // Collisionのアニメーションを実行. 142 143 animator.addBehavior(collision) 144 145 } 146 147 148 149 func onClickMyButton(sender : UIButton){ 150 151 152 153 switch(sender.tag) { 154 155 156 157 // DropButton. 158 159 case 0: 160 161 // Gravityのアニメーションを実行. 162 163 animator.addBehavior(gravity) 164 165 166 167 // ResetButton. 168 169 case 1: 170 171 172 173 let next = fourthViewController() 174 175 self.present(next, animated: false, completion: nil) 176 177 178 179 default: 180 181 print("error!") 182 183 } 184 185 } 186 187} 188
エラー個所は★★★★★の部分です。
Ambiguous use of 'addBoundaryWithIdentifier'
→「addBoundaryWithIdentifier」のあいまいな使用
とのことなのですが、解決方法がわかりませんでした。
どなたかお力をお貸しくださいませ。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/16 05:06
2019/08/16 05:46
2019/08/16 07:08