我是iOS开发的新手。 我有一个imageView.On点击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) {
}
}
答案 0 :(得分: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)