2015-10-30 27 views

回答

3

您需要在圖像視圖上使用輕觸手勢識別器,還需要將用戶交互屬性設置爲啓用。然後你可以從手勢識別器的方法中獲得點。下面是一些簡單的代碼:

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var imageView: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapAction:")) 

    self.imageView.userInteractionEnabled = true 
    self.imageView.addGestureRecognizer(tapGestureRecognizer) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func tapAction(sender: UITapGestureRecognizer) { 

    let touchPoint = sender.locationInView(self.imageView) // Change to whatever view you want the point for 
} 


} 
+0

我覺得這段代碼也會顯示tap的位置,即使在'UIImage'之外。除此之外,這是合法的! – Korpel

+1

@Korpel我不確定你的意思,手勢識別器被附加到imageView。 –

+0

確實是我的壞!它只會在'UIImag'內註冊水龍頭 – Korpel

0

UPDATE 2017年:

現在選擇器(字符串)已被棄用。可以使用新的語法#選擇器

另外,結尾不需要。

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))