简体   繁体   English

在tableView的Section Header中为imageView添加手势

[英]Add gesture for the imageView in tableView's Section Header

I want to implement a tableview with a header look like this . 我想实现一个表头,像这样的表视图。 I want to implement a function that it segue to another view controller when i press the icon in this header. 我想实现一个功能,当我按下此标题中的图标时,它会自动切换到另一个视图控制器。 But it seems the gesture recognizer doesn't work. 但是似乎手势识别器不起作用。 The recognizer would give no response at all 识别器完全不响应

The header's code is here 标头的代码在这里

  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
}

However, the iconTapGestureHandler function wouldn't be triggered after a tap the image view. 但是,点击图像视图后不会触发iconTapGestureHandler函数。

I have also implement the delegate method. 我还实现了委托方法。

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

the iconTapGestureHandler Function is as below iconTapGestureHandler函数如下

@IBAction func iconTapGestureHandler(sender:UIGestureRecognizer){
    println("in touch")
    let mainPageVC = MainPageTableViewController()
    mainPageVC.isMyself = false
    mainPageVC.isPushed = false
    self.navigationController?.pushViewController(mainPageVC, animated: true)
}

By default in UIImageView user interaction is disabled. 默认情况下,UIImageView中的用户交互处于禁用状态。 So add 所以加

imageView.userInteractionEnabled = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM