画像の場所を指定した中でランダムにしたい。
質問の内容がわかりずらくてゴメンなさい。
縦4 横4 の計16個の画像を配置してあります。
15こは同じ画像なのですが1つ違う画像にしてあります。
その違う1つをタッチするたびに違う場所に出現させたいのですがやり方がわかりません。
ランダムに再配置するやり方がわかりません
touchesBeganの所になんと書けばいいのでしょうか?
コードは以下のとうりです
見よう見まねでやったので雑でゴメンなさい。
class ViewController:UIViewController{
private var myImageView: UIImageView!
private var myImageView2: UIImageView!
private var myImageView3: UIImageView!
private var myImageView4: UIImageView!
private var myImageView5: UIImageView!
private var myImageView6: UIImageView!
private var myImageView7: UIImageView!
private var myImageView8: UIImageView!
private var myImageView9: UIImageView!
private var myImageView10: UIImageView!
private var myImageView11: UIImageView!
private var myImageView12: UIImageView!
private var myImageView13: UIImageView!
private var myImageView14: UIImageView!
private var myImageView15: UIImageView!
private var myImageView16: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myImageView = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage = UIImage(named: "images2.png")
myImageView.image = myImage
myImageView.layer.position = CGPoint(x: 60, y: 150)
self.view.addSubview(myImageView)
myImageView2 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage2 = UIImage(named: "images.png")
myImageView2.image = myImage2
myImageView2.layer.position = CGPoint(x: 140, y: 150)
self.view.addSubview(myImageView2)
myImageView3 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage3 = UIImage(named: "images.png")
myImageView3.image = myImage3
myImageView3.layer.position = CGPoint(x: 220, y: 150)
self.view.addSubview(myImageView3)
myImageView4 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage4 = UIImage(named: "images.png")
myImageView4.image = myImage4
myImageView4.layer.position = CGPoint(x: 300, y: 150)
self.view.addSubview(myImageView4)
myImageView5 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage5 = UIImage(named: "images.png")
myImageView5.image = myImage5
myImageView5.layer.position = CGPoint(x: 60, y: 250)
self.view.addSubview(myImageView5)
myImageView6 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage6 = UIImage(named: "images.png")
myImageView6.image = myImage6
myImageView6.layer.position = CGPoint(x: 140, y: 250)
self.view.addSubview(myImageView6)
myImageView7 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage7 = UIImage(named: "images.png")
myImageView7.image = myImage7
myImageView7.layer.position = CGPoint(x: 220, y: 250)
self.view.addSubview(myImageView7)
myImageView8 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage8 = UIImage(named: "images.png")
myImageView8.image = myImage8
myImageView8.layer.position = CGPoint(x: 300, y: 250)
self.view.addSubview(myImageView8)
myImageView9 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage9 = UIImage(named: "images.png")
myImageView9.image = myImage9
myImageView9.layer.position = CGPoint(x: 60, y: 350)
self.view.addSubview(myImageView9)
myImageView10 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage10 = UIImage(named: "images.png")
myImageView10.image = myImage10
myImageView10.layer.position = CGPoint(x: 140, y: 350)
self.view.addSubview(myImageView10)
myImageView11 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage11 = UIImage(named: "images.png")
myImageView11.image = myImage11
myImageView11.layer.position = CGPoint(x: 220, y: 350)
self.view.addSubview(myImageView11)
myImageView12 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage12 = UIImage(named: "images.png")
myImageView12.image = myImage12
myImageView12.layer.position = CGPoint(x: 300, y: 350)
self.view.addSubview(myImageView12)
myImageView13 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage13 = UIImage(named: "images.png")
myImageView13.image = myImage13
myImageView13.layer.position = CGPoint(x: 60, y: 450)
self.view.addSubview(myImageView13)
myImageView14 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage14 = UIImage(named: "images.png")
myImageView14.image = myImage14
myImageView14.layer.position = CGPoint(x: 140, y: 450)
self.view.addSubview(myImageView14)
myImageView15 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage15 = UIImage(named: "images.png")
myImageView15.image = myImage15
myImageView15.layer.position = CGPoint(x: 220, y: 450)
self.view.addSubview(myImageView15)
myImageView16 = UIImageView(frame: CGRectMake(0,0,60,60))
let myImage16 = UIImage(named: "images.png")
myImageView16.image = myImage16
myImageView16.layer.position = CGPoint(x: 300, y: 450)
self.view.addSubview(myImageView16)
myImageView.userInteractionEnabled = true
myImageView2.userInteractionEnabled = true
myImageView3.userInteractionEnabled = true
myImageView4.userInteractionEnabled = true
myImageView5.userInteractionEnabled = true
myImageView6.userInteractionEnabled = true
myImageView7.userInteractionEnabled = true
myImageView8.userInteractionEnabled = true
myImageView9.userInteractionEnabled = true
myImageView10.userInteractionEnabled = true
myImageView11.userInteractionEnabled = true
myImageView12.userInteractionEnabled = true
myImageView13.userInteractionEnabled = true
myImageView14.userInteractionEnabled = true
myImageView15.userInteractionEnabled = true
myImageView16.userInteractionEnabled = true
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}