简体   繁体   English

Xcode swift 3 不知道如何在我单击的位置显示图像

[英]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.

相关问题 我不知道如何在Swift中将屏幕截图发送到Instagram应用程序 - I don't know how to send screenshot to Instagram application in Swift Xcode中的致命错误-我知道原因,但不知道如何解决 - Fatal error in Xcode - I know the cause but don't know how to fix it Swift Stopwatch App崩溃,不知道我要去哪里 - Swift Stopwatch App crashing, don't know where I'm going wrong 不知道在 Swift 中使用 userDefaults 保存数据的 saveData 放在哪里 - Don't know where to place saveData to save data with userDefaults in Swift 我一直在迅速3中收到“信号1:SIGABRT”,但我不知道该如何解决 - I keep getting “signal 1: SIGABRT” in swift 3 and I don't know how to get around it 我不知道为什么在swift3中没有调用viewForHeaderInSection - I don't know why viewForHeaderInSection is not called in swift3 如果在运行前不知道本地图像的位置,该如何显示? - How can I display local images if I don't know their location before runtime? iOS组件会自动调整大小,但我不知道在哪里以及为什么 - iOS Component is automatically resizing but I don't know where and why Swift 3构建数组(不知道如何描述) - Swift 3 build array (Don't know how to describe) 不知道这个xcode错误是什么意思? - Don't know what this xcode error means?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM