I am a new programmer and I ran into a problem. I want an image to appear on the screen where I click. So for example I click on the top of the screen I want the image to show there and then if I click on the bottom of the screen I want the image to go there, but I don't want the first image to disappear I just want to add another one. Using swift 3 Thanks
You can simply achieve that by overriding touchesBegan
method of UIViewController
this way:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
guard let touch = touches.first else { return }
let touchLocation = touch.location(in: self.view) // Get the location of the touch
let image = UIImage(named: "anImage") // Create a UIImage instance
let imageView = UIImageView(image: image) // Create a UIImageView with your image
imageView.contentMode = .scaleAspectFit
// Set the position of your image to be where the user touched
imageView.frame = CGRect(x: touchLocation.x,
y: touchLocation.y,
width: 100,
height: 100)
self.view.addSubview(imageView) // Add the image to the view
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.