swift - 快速编程中点击的图像区域的坐标

标签 swift ios9 xcode7

我是 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/

相关文章:

ios - 添加 UIbutton 作为 subview 无法在按下时调用选择器

ios - 更新到 IOS11 和 Xcode9 后的问题!! "com.google.Maps.LabelingBehavior", "TIC Read Status"

swift - 获取 Playground 以显示所有循环结果

ios - 以编程方式更改对象的大小和位置

ios - 如何隐藏表格 View 上方的自定义 View

iOS Swift Dequeued Reusable Cell 未显示

ios - 如何在我的 iOS 9 应用程序中为静态 UIApplicationShortcutItem 指定自定义图标?

ios - 启用位码 - 为 iOS 9 做准备(xcode7 beta,支持 iOS 6)

ios9 - 'Unwind Segue' 可以在通过 Storyboard 引用连接的两个 Storyboard 之间工作吗?

ios - 使用 xcode 7.2 永远编译模型文件