ボタンを押すと画像が表示される。それら複数の画像をドラックするコードを書くことができません。
以下のコードでボタンを押すと画像を出現させることができました。
その画像をドラックしたいです!!
ViewController
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var Goalbutton: UIButton! 6 @IBOutlet weak var Missbutton: UIButton! 7 8 @IBOutlet var imageBeHereNow: UIImageView! 9 10 @IBAction func tapgoal(_ sender: Any) { 11 print("Goal") 12 // UIImageViewを作成 13 let maru = UIImageView(image: UIImage(named: "maru")) 14 let screenW:CGFloat = view.frame.size.width 15 let screenH:CGFloat = view.frame.size.height 16 maru.center = CGPoint(x:screenW/2, y:screenH/2) 17 self.view.addSubview(maru)// UIImageViewを追加 18 } 19 20 @IBAction func tapmiss(_ sender: Any) { 21 print("Shot Miss!!") 22 // UIImageViewを作成 23 let batu = UIImageView(image: UIImage(named: "batu")) 24 // スクリーンサイズの取得 25 let screenW:CGFloat = view.frame.size.width 26 let screenH:CGFloat = view.frame.size.height 27 batu.center = CGPoint(x:screenW/2, y:screenH/2) 28 self.view.addSubview(batu)// UIImageViewを追加 29 } 30 31 32 // @IBAction func addImage(_ sender: UITapGestureRecognizer) { 33 // let maru = UIImageView(image: UIImage(named: "maru")) 34 //maru.center = sender.location(in: self.view) 35 //self.view.addSubview(maru) 36 //} 37 38 override func viewDidLoad() { 39 super.viewDidLoad() 40 // Do any additional setup after loading the view. 41 self.view.addBackground(name: "court") 42 43 Goalbutton.setTitle("Goal",for:UIControl.State.normal) 44 Goalbutton.setTitleColor(UIColor.white, for: UIControl.State.normal) 45 46 Missbutton.setTitle("Shot Miss",for:UIControl.State.normal) 47 Missbutton.setTitleColor(UIColor.white, for: UIControl.State.normal) 48 49 } 50 51 52}
addBackground
1import UIKit 2 3extension UIView { 4 func addBackground(name: String) { 5 // スクリーンサイズの取得 6 let width = UIScreen.main.bounds.size.width 7 let height = UIScreen.main.bounds.size.height 8 9 // スクリーンサイズにあわせてimageViewの配置 10 let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height)) 11 //imageViewに背景画像を表示 12 imageViewBackground.image = UIImage(named: name) 13 14 // 画像の表示モードを変更。 15 imageViewBackground.contentMode = UIView.ContentMode.scaleAspectFill 16 17 // subviewをメインビューに追加 18 self.addSubview(imageViewBackground) 19 // 加えたsubviewを、最背面に設置する 20 self.sendSubviewToBack(imageViewBackground) 21 } 22}
色々とサイトを探したのですが、上手くいかなかったです。
あなたの回答
tips
プレビュー