タイトル通り、他の端末(端末A)でドラッグしたときの位置をDatabaseに保存して、それをリアルタイムでもう一方の端末(端末B)で取り込んで端末Aでドラッグした分だけ端末B上の画像を移動させたいです。
今のところ端末Aでドラッグした位置をDatabaseに保存しその値を端末Bでリアルタイムに取り込むところまではできました。
端末Aでの値の保存は以下のように記述しています
swift
1 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 2//画像の移動などのコードは省略しています 3 //x方向、y方向の移動分がdx,dyに入ってます 4// タッチイベントを取得 5 let touchEvent = touches.first! 6 7 // ドラッグ前の座標, Swift 1.2 から 8 let preDx = touchEvent.previousLocation(in: self.view).x 9 let preDy = touchEvent.previousLocation(in: self.view).y 10 11 // ドラッグ後の座標 12 let newDx = touchEvent.location(in: self.view).x 13 let newDy = touchEvent.location(in: self.view).y 14 15 // ドラッグしたx座標の移動距離 16 let dx = newDx - preDx 17 18 // ドラッグしたy座標の移動距離 19 let dy = newDy - preDy 20 21 //移動分をFirebaseに保存 22 self.ref.child("1").child("1").updateChildValues(["tapLocation_x": Int(dx)]) 23 self.ref.child("1").child("1")).updateChildValues(["tapLocation_y": Int(dy)]) 24}
このdx,dyをfired,firedyという変数に入れてその値分だけ端末Bでの画像を移動させようと以下のようにコードを書いたのですが下記のエラー箇所でエラーが出てしまいます。
swift
1override func viewDidAppear(_ animated: Bool) { 2 super.viewDidAppear(animated) 3 var AimageView : UIImageView! 4 var AFrame: CGRect = AimageView.frame 5 //firedxは上記の"tapLocation"のvalueをfirebaseから受け取った変数です 6 AFrame.origin.x += NumberFormatter().number(from: firedx) as! CGFloat//←エラー箇所 7 AFrame.origin.y += NumberFormatter().number(from: firedy) as! CGFloat 8 9 AimageView.frame = AFrame 10 11 self.view.addSubview(AimageView) 12}
とすると
Unexpectedly found nil while unwrapping an Optional value
というエラーが出てしまいます。
どのようにすれば出来るでしょうか。
情報など足りないところあるかもしれませんがご教授いただけませんでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。