将 tapGesture 添加到禁用 userInteraction 的 imageView 上的标签

Posted

技术标签:

【中文标题】将 tapGesture 添加到禁用 userInteraction 的 imageView 上的标签【英文标题】:Adding tapGesture to a label over an imageView whose userInteraction is disabled 【发布时间】:2017-06-02 10:04:50 【问题描述】:

我是 ios 开发新手。 我有一个 imageView。在点击 imageView 上的任何点时,我正在创建一个标签。在那之后,我想在点击新创建的标签时为该标签提供一个删除选项(这次我不想再次创建标签。所以我禁用了 imageView 的用户交互)。但是点击操作不起作用,因为我禁用了图像视图上的用户交互。当我启用它时,调用点击标签功能工作正常,但是一个新标签是正在补充这一点。 我附上下面的代码。我正在使用 swift 3.0 请帮帮我。

import UIKit

class Tagging: UIViewController,UISearchBarDelegate,UISearchControllerDelegate,UISearchResultsUpdating

    let tapGesture = UITapGestureRecognizer()
    let labelTapGesture = UITapGestureRecognizer()
    var taggedPersonNameLabel = UILabel()
    var pointingToNotation = TriangleView()
     var searchController : UISearchController!

    @IBOutlet var imageToBeTagged: UIImageView!
    override func viewDidLoad() 
        super.viewDidLoad()
        imageToBeTagged.image = imageToUploadAfterFiltering
        tapGesture.addTarget(self, action: #selector(tappedOnImage))
        imageToBeTagged.addGestureRecognizer(tapGesture)
        imageToBeTagged.isUserInteractionEnabled = true

        //creating done btn
        let next = UIButton(type: .custom)
        next.setTitle("Done", for: .normal)
        next.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
        next.addTarget(self, action: #selector(doneBtnAction), for: .touchUpInside)
        let nxtBtn = UIBarButtonItem(customView: next)

        self.navigationItem.rightBarButtonItem = nxtBtn

        //adding tap gesture to who's this label for giving delete tag action
        tapGesture.addTarget(self, action: #selector(deleteTag))

        taggedPersonNameLabel.isUserInteractionEnabled = true
        taggedPersonNameLabel.addGestureRecognizer(labelTapGesture)
    

    func doneBtnAction()

        let _ = self.navigationController?.popViewController(animated: true)
    

    func deleteTag() 
        print("oohhyaaaahhh")
    

    func tappedOnImage()

         let point = tapGesture.location(in: tapGesture.view)

        if point.x < 100 
             // triangle view class is defined in uitility  files folder
            pointingToNotation = TriangleView(frame: CGRect(x:point.x + 5, y:point.y - 3, width:10, height:6))
            pointingToNotation.backgroundColor = .clear
            self.imageToBeTagged.addSubview(pointingToNotation )
            //to show pop without going out beyond 0
             taggedPersonNameLabel = UILabel(frame : CGRect(x:point.x , y: point.y + 3,  width: 100, height: 25))

        else if point.x > 100 && point.x < UIScreen.main.bounds.width - 100

            pointingToNotation = TriangleView(frame: CGRect(x:point.x, y:point.y - 3, width:10, height:6))
            pointingToNotation.backgroundColor = .clear
            self.imageToBeTagged.addSubview(pointingToNotation )

        taggedPersonNameLabel = UILabel(frame : CGRect(x:point.x - 50  , y: point.y + 3,  width: 100, height: 25))

        
        else
            pointingToNotation = TriangleView(frame: CGRect(x:point.x - 15, y:point.y - 3, width:10, height:6))
            pointingToNotation.backgroundColor = .clear
            self.imageToBeTagged.addSubview(pointingToNotation )
        //to show pop up without going outside the screen width
            taggedPersonNameLabel = UILabel(frame : CGRect(x:point.x - 100  , y: point.y + 3,  width: 100, height: 25))

        
        taggedPersonNameLabel.textAlignment = .center
        taggedPersonNameLabel.textColor = UIColor.white
        taggedPersonNameLabel.layer.cornerRadius = 6
        taggedPersonNameLabel.layer.masksToBounds = true
        taggedPersonNameLabel.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        taggedPersonNameLabel.text = "Who's this?"
        self.imageToBeTagged.addSubview(taggedPersonNameLabel)

        self.searchController = UISearchController(searchResultsController:  nil)

        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self
        self.searchController.searchBar.searchBarStyle = .minimal
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true
        self.searchController.searchBar.tintColor = UIColor.white
        self.navigationItem.titleView = searchController.searchBar
        self.definesPresentationContext = true

        let textFieldInsideSearchBar = self.searchController.searchBar.value(forKey: "searchField") as? UITextField

        textFieldInsideSearchBar?.textColor = UIColor.white
        self.imageToBeTagged.isUserInteractionEnabled = false

    

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) 
        //this is used to hide search bar.
        // self.searchController.searchBar.isHidden = true

    

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) 


    

    func updateSearchResults(for searchController: UISearchController) 

    

【问题讨论】:

【参考方案1】:

tapGesture添加两次手势目标,将其更改为labelTapGesture

    imageToBeTagged.image = imageToUploadAfterFiltering
    //added the action to tap on image          
    tapGesture.addTarget(self, action: #selector(tappedOnImage))

    imageToBeTagged.addGestureRecognizer(tapGesture)
    imageToBeTagged.isUserInteractionEnabled = true

    //creating done btn
    let next = UIButton(type: .custom)
    next.setTitle("Done", for: .normal)
    next.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
    next.addTarget(self, action: #selector(doneBtnAction), for: .touchUpInside)
    let nxtBtn = UIBarButtonItem(customView: next)

    self.navigationItem.rightBarButtonItem = nxtBtn

    //adding tap gesture to who's this label for giving delete tag action
    //label tap to delete the tag
    labelTapGesture.addTarget(self, action: #selector(deleteTag))//change hear

    taggedPersonNameLabel.isUserInteractionEnabled = true
    taggedPersonNameLabel.addGestureRecognizer(labelTapGesture)

【讨论】:

谢谢兄弟的回复..但是我运气不好 然后将手势添加到新创建的标签

以上是关于将 tapGesture 添加到禁用 userInteraction 的 imageView 上的标签的主要内容,如果未能解决你的问题,请参考以下文章

点击手势关闭键盘似乎禁用 didSelectRow

将 UITapGesture 添加到 UIScrollView

iOS TapGesture 不工作

swift自定义上下文菜单previewprovider不能点击里面的任何视图(使用tapgesture)

UITextField TapGesture 在 iOS 7.1 上没有响应

如何在 UIView 中为 UIImageView 添加 tapGesture