我是 Swift 应用开发新手,我想知道如何使用 Swift 根据单击 Image
的位置添加触摸事件。我需要获取图像被点击区域的坐标。
最佳答案
您需要在 ImageView 上使用点击手势识别器,还需要将用户交互属性设置为启用。然后你可以从手势识别器的方法中得到要点。这是一些快速代码:
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
}
}
关于swift - 快速编程中点击的图像区域的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427110/