简体   繁体   English

Swift:以编程方式添加 UITapGestureRecognizer 不起作用

[英]Swift: programmatically add UITapGestureRecognizer doesn't work

I have this code block, but it does not work the way I want to.我有这个代码块,但它不像我想要的那样工作。 datum_view appear good, but to no avail I click on it, it does not work. datum_view 看起来不错,但我点击它无济于事,它不起作用。

Update code:更新代码:

Position位置

var containerView: UIView!
var tov_datum = [String]()
var datum_view: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    containerView = UIView()

    let SCREEN_SIZE = UIScreen.mainScreen().bounds


    var Y: CGFloat = 0

    for index in tov_datum {
        datum_view = UIView(frame: CGRectMake(0, Y, SCREEN_SIZE.width, 80))
        let gradientView = GradientView(frame: datum_view.bounds)
        datum_view.insertSubview(gradientView, atIndex: 0)

        let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:"klikk:")
        datum_view.userInteractionEnabled = true
        datum_view.addGestureRecognizer(tapGestureRecognizer)

        containerView.addSubview(datum_view)


        Y = Y + 80
    }
    let YY = tov_datum.count * 80
    NSLayoutConstraint(item: vetítésekView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1.0, constant:CGFloat(YY)).active = true

    vetítésekView.addSubview(containerView)
}


func klikk(recognizer: UITapGestureRecognizer) {
    print("BOOOOYAAAA")
}

Doesn't look like the size of containerView is set anywhere.看起来没有在任何地方设置containerView的大小。 To quickly see if that's the problem, initialize it with the frame you're creating datum_view inside:要快速查看这是否是问题所在,请使用您在其中创建datum_view的框架对其进行初始化:

let SCREEN_SIZE = UIScreen.mainScreen().bounds
containerView = UIView(frame: SCREEN_SIZE)

If that works, then figure out what size you actually want containerView to be and set up appropriate constraints.如果可行,那么确定您实际希望 containerView 的大小并设置适当的约束。

Change改变

func klikk(recognizer: UITapGestureRecognizer)

to

func klikk(_ recognizer: UITapGestureRecognizer)

Change this line :改变这一行:

let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("klikk:"))

to :到 :

let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:"klikk:")

您应该将 tapGestureRecognizer 声明为属性。

暂无
暂无

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

相关问题 Scroll View中用于UIView的UITapGestureRecognizer不起作用 - UITapGestureRecognizer for UIView in Scroll View doesn't work 自定义 UIView 中的 UITapGestureRecognizer 不起作用 - UITapGestureRecognizer in custom UIView doesn't work addSubview 在 UIView - Swift 中不起作用 - addSubview doesn't work in UIView - Swift Programmatically UITapGestureRecognizer在UICollectionView标头上不起作用 - UITapGestureRecognizer doesn't work on UICollectionView header 使用UITapGestureRecognizer在UIView之上的UIButton不起作用 - UIButton on top of UIView with UITapGestureRecognizer doesn't work tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)将UITapGestureRecognizer添加到自定义UITableViewCell时不起作用 - tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) Doesn't work when I add a UITapGestureRecognizer to my custom UITableViewCell 当 TTTAttributedLabel 上有一个 UITapGestureRecognizer 时,点击 URL 在 TTTAttributedLabel 中不起作用 - Tapping URL doesn't work in TTTAttributedLabel while there is a UITapGestureRecognizer on its superview Obj-C-在对UIView进行动画处理后,UITapGestureRecognizer无法正常工作吗? - Obj-C - UITapGestureRecognizer doesn't work after UIView is animated? 自定义表格视图单元格上的 UITapGestureRecognizer 不起作用 - UITapGestureRecognizer on custom table view cell doesn't work 快速地以编程方式创建的firebase动态链接不起作用,没有域 - firebase dynamic link created programmatically in swift doesn't work, no domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM