2017-10-28 85 views
-4

我是一名新程序員,遇到問題。我想讓圖片出現在我點擊的屏幕上。因此,例如,我點擊屏幕的頂部,我希望圖像在那裏顯示,然後如果我點擊屏幕底部,我希望圖像去那裏,但我不想第一個圖像消失,我只是想要添加另一個。使用SWIFT 3 感謝Xcode swift 3不知道如何顯示圖片,點擊

+0

你應該表現出一些代碼 –

+0

@ MoeAbdul - 哈米德我可以做一個按鈕顯示圖像,但是這不是我想要什麼,我希望能夠點擊屏幕上的某個地方,並讓我的圖像顯示我點擊的位置。我沒有任何代碼,因爲我不確定從哪裏開始。 –

回答

0

你可以簡單地實現,通過覆蓋的UIViewControllertouchesBegan方法是這樣的:

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 
}