编辑代码

func displayArrayGraphButton() {
    let myView1 = self.view.viewWithTag(2001) as! UIImageView
    let myView2 = self.view.viewWithTag(2002) as! UIImageView
    myView1.backgroundColor = .darkGray
    myView2.backgroundColor = .darkGray
    //支持View可以进行交互等操作
    myView1.isUserInteractionEnabled = true
    myView2.isUserInteractionEnabled = true

    let h = myView1.frame.size.height
    let w = myView2.frame.size.width

    let padding : CGFloat = 5
    let margin : CGFloat = 10

    //计算每个格子的高度和宽度
    let grid_w = (w-margin * 2-padding * CGFloat(cols-1)) / CGFloat
    let grid_h = (h-margin * 2-padding * CGFloat(rows-1)) / CGFloat

    for i in 0..<rows{
        for j in 0..<cols{
            let x = margin + CGFloat(j) * (grid_w + padding)
            let y = margin + CGFloat(i) * (grid_h + padding)
            let rect = CGRect(x: x, y: y, width: grid_w, height: grid_h)
            //在上面的ImageView中显示
            let fileName1 = iv1[i*cols+j]
            let img1 = UIImage(named: fileName1)
            let btn1 = UIButton(frame: rect)
            btn1.setImage(img1, for: .normal)
            btn1.backgroundColor = .yellow
            //Button的tag设定为4000及以上
            btn1.tag = i*cols+j + 4000
            btn1.addTarget(self, action: #selector(buttonCheck(_:)),for: .touchUpInside)
            myView1.addSubview(btn1)
            //在下面的ImageView中显示
            let fileName2 = iv2[i*cols+j]
            let img2 = UIImage(named: fileName2)
            let btn2 = UIButton(frame: rect)
            btn2.setImage(img2, for: .normal)
            btn2.backgroundColor = .yellow
            //Button的tag设定为5000及以上
            btn2.tag = i*cols+j + 5000
            btn2.addTarget(self, action: #selector(buttonCheck(_:)),for: .touchUpInside)
            myView2.addSubview(btn2)
        }
    }
}

//函数4:不同点的Button被单击后调用
@objc func buttonCheck(_ sender : UIButton) {
    let tipsLabel = self.view.viewWithTag(1001) as! UILabel
    let tag = semder.tag
    if tag >= 5000 {
        let row = Int ((tag - 5000) / cols)
        let col = tag - 5000 - cols * row
        print("Bottom(\(row),\(col))")
        tipsLabel.text = "Bottom(\(row),\(col))"
    }
    else if tag >= 4000 {
        let row = Int ((tag - 4000) / cols)
        let col = tag - 4000 - cols * row
        print("Top(\(row),\(col))")
        tipsLabel.text = "Top(\(row),\(col))"
    }
    else {
        print("Button.tag=\(sender.tag)")
        tipsLabel.text = "Button.tag=\(sender.tag)"
    }
}

//函数5:生成干扰项的随机位置和随机内容
func distractorCreate(mount:Int) {
    for _ in 0..<mount {
        let col = Int(arc4random()) % cols
        let row = Int(arc4random()) % rows

        cord.append((row,col))
        //从images提供的字符中选一个
        let index = Int(arc4random()) % images.count
        //影响iv1和iv2这两个图形
        iv1[cols*row + col] = images[index]
        iv2[cols*row + col] = images[index]
    }
}

//数据定义开始--
//定义行数和列数
let rows = 8
let cols = 8
//定义上下两个图形数组
var iv1 : Array<String> = []
var iv2 : Array<String> = []
//定义上下相同的干扰项数组
var cords : Array<Int,Int> = []
//干扰项和不同项只能是小图形
let images :Array<String> = ["bird","bee","flower","mogu"]
//数据定义结束--