UITapGestureRecognizer called immediately

前端 未结 2 2022
故里飘歌
故里飘歌 2021-01-21 04:22

I am trying to add a UITapGestureRecognizer to a imageview as shown below

let gest=UITapGestureRecognizer(target: self, action: Selecto         


        
相关标签:
2条回答
  • 2021-01-21 05:00

    Change your gesture line like following if swift 2.0 or less

    let gest=UITapGestureRecognizer(target: self, action: Selector("imgPressed"));
    

    if you are using swift 2.2 or greater than that

    let gest=UITapGestureRecognizer(target: self, action: #selector(imgPressed));
    

    Hope this will help you.

    0 讨论(0)
  • 2021-01-21 05:09

    You should use following syntax:

    let gest = UITapGestureRecognizer(target: self, action: #selector(imgPressed))
    

    Note, that Selector can accept initial type Void as well as String. Properly old-style Selector initialization looks like this:

    let action: Selector = Selector("imgPressed")
    

    Now you may check, that if you will try combine new style with old style you will get compilation error:

    let action = Selector(imgPressed) // Error!
    

    Funny thing is that you probably will try to resolve this error by appending bracers, and you will not have any errors after that! But this is completely useless. Look at following 3 lines of code, they are equivalent

    let action = Selector(imgPressed())
    let action = Selector(Void())
    let action = Selector()
    

    exept one thing - in first line you called imgPressed() function, returned Void, by your hands. Thats it =)

    0 讨论(0)
提交回复
热议问题