Ios 在禁用用户交互的imageView上向标签添加Tap手势
我是iOS开发新手。 我有一个imageView。在点击imageView上的任何一点时,我正在该点上创建一个标签。之后,我想在点击新创建的标签时为该标签提供一个删除选项(这次我不想再次创建标签。因此我禁用了imageView的用户交互)。但是点击操作不起作用,因为我禁用了图像视图上的userinteraction。启用它后,调用标签上的点击功能可以正常工作,但此时正在添加新标签。 我在下面附上代码。我用的是swift 3.0 请帮帮我Ios 在禁用用户交互的imageView上向标签添加Tap手势,ios,swift,uiimageview,uilabel,uitapgesturerecognizer,Ios,Swift,Uiimageview,Uilabel,Uitapgesturerecognizer,我是iOS开发新手。 我有一个imageView。在点击imageView上的任何一点时,我正在该点上创建一个标签。之后,我想在点击新创建的标签时为该标签提供一个删除选项(这次我不想再次创建标签。因此我禁用了imageView的用户交互)。但是点击操作不起作用,因为我禁用了图像视图上的userinteraction。启用它后,调用标签上的点击功能可以正常工作,但此时正在添加新标签。 我在下面附上代码。我用的是swift 3.0 请帮帮我 import UIKit class Tagging:
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) {
}
}
导入UIKit
类标记:UIViewController、UISearchBarDelegate、UISearchControllerDelegate、UISearchResultsUpdated{
让TapSignature=UITapGestureRecognizer()
让LabelTap手势=UITapGestureRecognitizer()
var taggedpersonnamelab=UILabel()
var pointingToNotation=TriangleView()
var searchController:UISearchController!
@IBVAR ImageToBetaged:UIImageView!
重写func viewDidLoad(){
super.viewDidLoad()
ImageToBetaged.image=imageToUploadAfterFiltering
点击手势。添加目标(自我,动作:#选择器(点击动画))
ImageToBetaged.AddGestureRecognitor(点击手势)
ImageToBetaged.isUserInteractionEnabled=true
//创建完成的btn
let next=ui按钮(类型:。自定义)
next.setTitle(“完成”,用于:。正常)
next.frame=CGRect(x:0,y:0,宽度:60,高度:30)
next.addTarget(self,action:#选择器(doneBtnAction),for:.touchUpInside)
设nxtBtn=UIBarButtonItem(customView:next)
self.navigationItem.rightBarButtonItem=nxtBtn
//将点击手势添加到此标签的“谁是”以执行删除标记操作
点击手势。添加目标(自我,动作:#选择器(deleteTag))
taggedPersonNameLabel.isUserInteractionEnabled=true
taggedPersonNameLabel.AddGestureRecognitor(LabelTap手势)
}
func doneBtnAction(){
let=self.navigationController?.popViewController(动画:true)
}
func deleteTag(){
打印(“Oohhyaaah”)
}
func tappedOnImage(){
let point=Tap手势.位置(在:Tap手势.视图中)
如果点x<100{
//三角形视图类在uitility files文件夹中定义
pointingToNotation=TriangleView(帧:CGRect(x:point.x+5,y:point.y-3,宽度:10,高度:6))
pointingtonotion.backgroundColor=.clear
self.imageToBetaged.addSubview(PointingToStation)
//在不超过0的情况下显示pop
taggedPersonNameLabel=UILabel(帧:CGRect(x:point.x,y:point.y+3,宽度:100,高度:25))
}否则,如果point.x>100&&point.x
u为点击手势添加两次手势目标
将其更改为标签手势
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)
谢谢你的回复,兄弟。但是我的坏运气在新标签上添加了手势