[英]Adding Tap Gesture to any view triggers nothing
I am dealing with the single most frustrating issue so far in my journey to iOS development. 到目前为止,我正在处理iOS开发中最令人沮丧的问题。 I originally was trying to add a gesture recognizer to my
UIImage
view but was not having any luck. 我最初试图在我的
UIImage
视图中添加一个手势识别器,但是没有任何运气。 I did some searching on stack and found that I hadn't set imageView.isUserInteractionEnabled = true
which I figured would solve my problem but it didn't. 我在堆栈上进行了一些搜索,发现我没有设置
imageView.isUserInteractionEnabled = true
,我认为这可以解决我的问题,但事实并非如此。 So then I started adding gesture recognizers to everything including the parent view of imageView
but still got nothing. 因此,我开始将手势识别器添加到所有内容,包括
imageView
的父视图,但仍然一无所获。 I am sure that something I am doing/haven't done is so simple but I have just totally missed it. 我敢肯定,我正在做/尚未做的事情是如此简单,但是我完全错过了它。 Please help.
请帮忙。
ProfileViewController: UIViewController {
// User's info container (Parent iof imageView)
let userInfoContainer: UIView = {
let head = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
head.backgroundColor = UIColor.white
head.translatesAutoresizingMaskIntoConstraints = false
head.layer.borderWidth = 1
print(head.isUserInteractionEnabled) // Returns true
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
head.addGestureRecognizer(tap)
return head
}()
// Users name
let userName: UITextField = {
let name = UITextField()
name.text = "John Doe"
name.translatesAutoresizingMaskIntoConstraints = false
name.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
name.addGestureRecognizer(tap)
return name
}()
// User's profile image
let profileImageView: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "avatar"))
imageView.layer.cornerRadius = 50
imageView.layer.masksToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))
return imageView
}()
//
//
// skipping other UI elements
//
//
override func viewDidLoad() {
super.viewDidLoad()
// Authentication bits
self.title = "Profile View"
self.view.backgroundColor = UIColor.white
self.view.addSubview(userInfoContainer)
self.view.addSubview(profileImageView)
self.view.addSubview(userName)
} // end viewDidLoad
func tapped() {
print(123)
}
// setup constraints
func setupUserInfoContainer() {
userInfoContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
userInfoContainer.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
userInfoContainer.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
// profile image arrangement
func setupProfileImage() {
profileImageView.leftAnchor.constraint(equalTo: userInfoContainer.leftAnchor, constant: 20).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: userInfoContainer.centerYAnchor).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
func setupLabels() {
userName.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 20).isActive = true
userName.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
userName.heightAnchor.constraint(equalToConstant: 20).isActive = true
userName.isEnabled = false
}
} // end view controller
Rendered View: 渲染视图:
View Hierarchy
查看层次结构
Extra information that is likely not necessary: ProfileViewController is being handled by a UITabBarController but from what I have seen, that shouldn't make a difference.
可能不需要的额外信息:UITabBarController正在处理ProfileViewController,但是从我所看到的来看,这没有什么区别。
Update From looking around it looks like the appropriate swift 3 syntax is let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
but I think having that line in the closure is what's causing this error to be thrown ../ProfileViewController.swift:37:74: Value of type '(NSObject) -> () -> ProfileViewController' has no member 'tapped'
从环顾四周更新看起来像是适当的swift 3语法,
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
但我认为关闭中的那一行是导致此错误的原因被抛出../ProfileViewController.swift:37:74: Value of type '(NSObject) -> () -> ProfileViewController' has no member 'tapped'
If someone could explain how I can fix this that would be stellar. 如果有人可以解释我如何解决这个问题,那将是出色的。
I don't believe you can assign a gesture recognizer in this manner... 我不相信您可以通过这种方式分配手势识别器...
1 let profileImageView: UIImageView = {
2 let imageView = UIImageView(image: #imageLiteral(resourceName: "avatar"))
3 imageView.layer.cornerRadius = 50
4 imageView.layer.masksToBounds = true
5 imageView.translatesAutoresizingMaskIntoConstraints = false
6 imageView.isUserInteractionEnabled = true
7 imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))
8 return imageView
9 }()
On line 7, what is self
? 在第7行,
self
什么? It is the function that is returning a UIImageView
. 它是返回
UIImageView
的函数 。
Your func tapped()
is not part of that function... it belongs to ProfileViewController
. 您的
func tapped()
不属于该函数...它属于ProfileViewController
。
You might be able to find a way to change the target, but a couple minutes of trying different approaches has not yielded any luck for me. 您也许可以找到改变目标的方法,但是尝试不同方法的几分钟没有给我带来任何运气。
I think you need to create and add the GestureRecognizer(s) inside ProfileViewController / viewDidLoad()
(or elsewhere within that class). 我认为您需要在
ProfileViewController / viewDidLoad()
(或该类中的其他位置)内部创建并添加GestureRecognizer。
Why don't you just initiliaise your image view at the top of the class like 您为什么不像在班级顶部那样初始化图像视图
let profileImageView = UIImageView()
and create a function to configure the imageview and add it as a subview such in view did load. 并创建一个功能来配置imageview并将其添加为子视图,例如在view中加载。 That should work
那应该工作
func configureImageView() {
#add methods and customize image view
#add image view to sub view
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.