Here is the code that is just about adding an image view programmatically:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageName = "thisIsAnImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
view.addSubview(imageView)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// change position of image
}
}
That is how I know how to add an image view programmatically now.
I could change the position by writing code inside the viewDidLoad-function, but this function only runs at the beginning of running the app.
How to write the code (that is for adding the image view) outside the viewDidLoad-function in order to access the image and change the position by tapping?