美文网首页
swift Uivew 添加点击事件

swift Uivew 添加点击事件

作者: 大鹏你我他 | 来源:发表于2021-07-08 16:34 被阅读0次

        privatestructAssociateKeys {

            staticvargestureKey ="gestureKey"

            staticvartapBlock ="tapBlock"

        }

        func yx_setTapActionWithBlock(tapBlock:@escaping() ->Void){

            self.isUserInteractionEnabled = true

            var tapGesture = objc_getAssociatedObject(self, &AssociateKeys.gestureKey) as? UITapGestureRecognizer

            if(tapGesture==nil){

                tapGesture =UITapGestureRecognizer.init(target:self, action:#selector(handleActionForTapGesture(_:)))

                tapGesture!.numberOfTapsRequired=1

                self.addGestureRecognizer(tapGesture!)

                objc_setAssociatedObject(self, &AssociateKeys.gestureKey, tapGesture, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

            }

            objc_setAssociatedObject(self, &AssociateKeys.tapBlock, tapBlock, .OBJC_ASSOCIATION_RETAIN_NONATOMIC);

        }

        @objcfunchandleActionForTapGesture(_gesture:UITapGestureRecognizer) {

            lettapBlock =objc_getAssociatedObject(self, &AssociateKeys.tapBlock)as? () ->Void

            if(tapBlock!=nil) {

                tapBlock!()

            }

        }

    相关文章

      网友评论

          本文标题:swift Uivew 添加点击事件

          本文链接:https://www.haomeiwen.com/subject/amjtpltx.html