gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:35:37 27 4
gpt4 key购买 nike

我是 iOS 开发新手。我有一个 imageView。点击 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) {

}
}

最佳答案

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)

关于ios - 将 tapGesture 添加到禁用了 userInteraction 的 imageView 上的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44326232/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com