0
我想調整一些scrollview方法爲我的集合/ tableview來跟蹤一些事件。我收到錯誤,因爲它無法在collectionview中找到scrollViewDidScroll:Method。我試着用下面的代碼如何調整ScrollViewDidScroll或UICollectionView的其他滾動視圖方法
extension UICollectionView
{
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
// make sure this isn't a subclass
if self !== UICollectionView.self {
return
}
dispatch_once(&Static.token) {
let originalSelector = #selector(self.scrollViewDidScroll(_:))
let swizzledSelector = #selector(self.sp_scrollViewDidScroll(_:))
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
// MARK: - Method Swizzling
func sp_scrollViewDidScroll(scrollView:UIScrollView) {
self.sp_scrollViewDidScroll(animated)
print("Swizzled sp_scrollViewDidScroll in place of scrollViewDidScroll")
}
}
Error: Type UICollectionView has no member 'scrollViewDidScroll'
有沒有辦法改變委託方法? –
它們就像任何其他方法一樣,所以是的......但是因爲這使用委託來將實現卸載到您選擇的類/對象上,所以交換實現的想法完全是多餘的,只是實現它如何在需要時完成你的代表和中提琴。 – Jef