[英]Xcode swift 3 don't know how to display an image where I click
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
使用 swift 3 谢谢
You can simply achieve that by overriding touchesBegan
method of UIViewController
this way:您可以通过以这种方式覆盖
UIViewController
touchesBegan
方法来简单地实现这一点:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.