我想实现一个表格,其标题看起来像this。我想实现一个功能,当我按下此标题中的图标时,它会转到另一个视图控制器。 但似乎手势识别器不起作用。识别器根本不会给出任何响应
标题的代码在这里
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UITableViewHeaderFooterView(frame:CGRect(x: 0, y: 0, width: frameWidth, height: headerViewHeight))
let iconOffset = CGPoint(x: 8, y: 8)
let iconSize = CGSize(width: headerViewHeight-16, height: headerViewHeight-16)
let imageView = UIImageView(frame: CGRect(origin: iconOffset, size: iconSize))
imageView.image = UIImage(named: "HENRY")
imageView.layer.cornerRadius = (headerViewHeight-16)/2
imageView.layer.masksToBounds = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("iconTapGestureHandler:"))
// tapRecognizer.cancelsTouchesInView = false
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.delegate = self
imageView.addGestureRecognizer(tapRecognizer)
headerView.addSubview(imageView)
return headerView
}
但是,点击图片视图后,不会触发iconTapGestureHandler函数。
我还实现了委托方法。
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
iconTapGestureHandler功能如下
@IBAction func iconTapGestureHandler(sender:UIGestureRecognizer){
println("in touch")
let mainPageVC = MainPageTableViewController()
mainPageVC.isMyself = false
mainPageVC.isPushed = false
self.navigationController?.pushViewController(mainPageVC, animated: true)
}
答案 0 :(得分:3)
默认情况下,UIImageView禁用用户交互。所以添加
imageView.userInteractionEnabled = true