###(Swift)二つの画像を同時に動かしたいです.
現在,ipadで使う顕微鏡に関するアプリケーションを作っています.
そこで,顕微鏡のプレパラートの動きと見え方を対応させるようなものを考えているのですが,うまくいきません.
具体的には,ビューの中に円形のコンテナビューを入れて,そこに表示させる画像の動きをビューに置いてある画像のドラッグによる動きと合わせたいのです.
それぞれ別々に操作することはできるのですが,ビューに置いてある画像の動きにコンテナビューの中にある画像の動きを対応させるにはどうすればいいのでしょうか.
初心者なので,伝わりにくい質問かとは思いますが,宜しくお願い致します.
###発生している問題・エラーメッセージ
###書いたソースコード
完全に初心者なので,とりあえず画像を動かせるコードだけ調べてコピペしました.
import UIKit
class ViewController: UIViewController {
let image = UIImageView() override func viewDidLoad() { super.viewDidLoad() image.image = UIImage(named: "1.JPG") // 画像のフレームを設定 image.frame = CGRect(x:0, y:0, width:304, height:304) // タッチ操作を enable image.isUserInteractionEnabled = true self.view.addSubview(image) } // 画面にタッチで呼ばれる override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { print("touchesBegan") } // ドラッグ時に呼ばれる override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { // タッチイベントを取得 let touchEvent = touches.first! // ドラッグ前の座標, Swift 1.2 から let preDx = touchEvent.previousLocation(in: self.view).x let preDy = touchEvent.previousLocation(in: self.view).y // ドラッグ後の座標 let newDx = touchEvent.location(in: self.view).x let newDy = touchEvent.location(in: self.view).y // ドラッグしたx座標の移動距離 let dx = newDx - preDx print("x:\(dx)") // ドラッグしたy座標の移動距離 let dy = newDy - preDy print("y:\(dy)") // 画像のフレーム var viewFrame: CGRect = image.frame // 移動分を反映させる viewFrame.origin.x += dx viewFrame.origin.y += dy image.frame = viewFrame self.view.addSubview(image) } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { print("End") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
###試したこと
ビューにある画像の座標情報をコンテナビューにある画像に常に送れればいいのかなと思い,その方法も調べたのですが,私の理解力が足りずよく分からない結果となりました.
###補足情報(言語/FW/ツール等のバージョンなど)
swift/storyboard