到目前为止,我正在处理iOS开发之旅中最令人沮丧的问题。我最初试图在我的UIImage
视图中添加手势识别器,但没有任何运气。我在堆栈上做了一些搜索,发现我没有设置imageView.isUserInteractionEnabled = true
,我认为这可以解决我的问题,但事实并非如此。那么我开始向所有内容添加手势识别器,包括imageView
的父视图,但仍然没有任何内容。我确信我正在做的事情/未完成的事情是如此简单,但我完全错过了它。请帮忙。
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
渲染视图: 查看层次结构 可能不需要的额外信息:ProfileViewController正在由UITabBarController处理,但从我所看到的情况来看,这不应该有所作为。
更新
从环顾四周看起来似乎是合适的swift 3语法
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
但我认为在闭包中使用该行会导致抛出此错误../ProfileViewController.swift:37:74: Value of type '(NSObject) -> () -> ProfileViewController' has no member 'tapped'
如果有人能够解释我如何能够解决这个问题,那就太棒了。
答案 0 :(得分:1)
我不相信你能以这种方式分配手势识别器......
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 }()
第7行,self
是什么? 函数正在返回UIImageView
。
您的func tapped()
不属于该功能......它属于ProfileViewController
。
你可能能够找到改变目标的方法,但是几分钟尝试不同的方法并没有给我带来任何好运。
我认为您需要在ProfileViewController / viewDidLoad()
(或该类中的其他位置)内创建和添加GestureRecognizer。
答案 1 :(得分:0)
问题是你没有将委托设置为点击手势。查看this
希望这会起作用
答案 2 :(得分:0)
为什么不直接在课堂顶部初始化你的图像视图,如
let profileImageView = UIImageView()
并创建一个函数来配置imageview并将其添加为子视图,例如在视图中加载。这应该工作
func configureImageView() {
#add methods and customize image view
#add image view to sub view
}