Swift 统计项目中所有按钮的点击次数
class Swizzle: NSObject { override class func load() { UIButton.xxx_swizzleSendAction() } } extension UIButton { class func xxx_swizzleSendAction() { struct xxx_swizzleToken { static var onceToken : dispatch_once_t = 0 } dispatch_once(&xxx_swizzleToken.onceToken, { () -> Void in let cls : AnyClass! = UIButton.self let originalSelector = Selector("sendAction:to:forEvent:") let swizzledSelector = Selector("xxx_sendAction:to:forEvent:") let originalMethod = class_getInstanceMethod(cls, originalSelector) let swizzledMethod = class_getInstanceMethod(cls, swizzledSelector) method_exchangeImplementations(originalMethod, swizzledMethod) }) } public func xxx_sendAction(action : Selector, to : AnyObject!, forEvent : UIEvent!) { struct xxx_buttonTapCount { static var count : Int = 0 } xxx_buttonTapCount.count += 1 println(xxx_buttonTapCount.count) xxx_sendAction(action, to: to, forEvent: forEvent) } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。