繁体   English   中英

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

[英]Xcode swift 3 don't know how to display an image where I click

我是一名新程序员,遇到了一个问题。 我想要一个图像出现在我点击的屏幕上。 因此,例如我单击屏幕顶部我希望图像显示在那里然后如果我单击屏幕底部我希望图像去那里,但我不希望第一张图像消失我只是想添加另一个。 使用 swift 3 谢谢

您可以通过以这种方式覆盖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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM