2015-10-30 24 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))