我正在尝试在UIImageView
时从UIImage
的touchesBegan
获取字符串值。
这是整个viewcontroller代码。
import UIKit
class ViewController: UIViewController {
//MARK: Outlets
@IBOutlet weak var imageOne: UIImageView!
@IBOutlet weak var imageTwo: UIImageView!
@IBOutlet weak var myText: UILabel!
let imagesArray = ["banaan", "auto", "kip", "boom" ]
var questionArray: [String] = []
var touchArray: [CGPoint] = []
var itemOne: Int!
var itemTwo: Int!
override func viewDidLoad() {
super.viewDidLoad()
setupQuestions()
setupImages()
}
//MARK: Randomizing The Lable Question
fileprivate func setupQuestions() {
let firstRandomizer = arc4random_uniform(UInt32(imagesArray.count))
let secondRandomizer = arc4random_uniform(UInt32(imagesArray.count))
itemOne = Int(firstRandomizer)
itemTwo = Int(secondRandomizer)
if itemOne == itemTwo && itemTwo != 0 {
itemTwo -= 1
} else if itemOne == itemTwo && itemTwo == 0 {
itemTwo += 1
}
myText.text = """
selecteer de \(imagesArray[itemOne].capitalized)
voor dat je de \(imagesArray[itemTwo].capitalized) selecteert
"""
}
//Mark: setup images and randomizeing
fileprivate func setupImages() {
imageOne.image = UIImage(named: imagesArray[itemOne])
imageTwo.image = UIImage(named: imagesArray[itemTwo])
}
//MARK: Button Randomizer
@IBAction func nextBtnWasPressed(_ sender: Any) {
setupQuestions()
setupImages()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//Get image stringvalue to compare it with the question array
}
}
最佳答案
你可以试试
self.myImageView.image = UIImage(myImageArray[0])
self.myImageView.tag = 0
然后内在的触动
myImageArray[self.myImageView.tag]
您还可以在下面声明一个var以获取良好的模式
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("itemOne name \(myImageArray[itemOne]) , itemTwo name \(myImageArray[itemTwo])")
}