UITapGestureRecognizer called immediately

前端 未结 2 2021
故里飘歌
故里飘歌 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: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 =)

提交回复
热议问题