我正在尝试为UIScrollViewDelegate
方法实现默认实现:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
使用protocol
。
如果我把这个方法放在类中,就会调用它;但是,如果我尝试通过protocol
使用默认实现,它永远不会被调用。
码:
protocol DefaultScrollViewEndDragging {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
}
extension DefaultScrollViewEndDragging {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("scrollViewWillEndDragging is called")
// THIS IS NEVER CALLED
}
}
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, DefaultScrollViewEndDragging {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
print("Cell \(indexPath.row)")
cell.contentView.backgroundColor = .orange
return cell
}
}
我究竟做错了什么?
我觉得你在找这样的东西
protocol DefaultScrollViewEndDragging : UIScrollViewDelegate {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
}
extension DefaultScrollViewEndDragging {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("scrollViewWillEndDragging is called")
// THIS IS NEVER CALLED
}
}
遗憾的是,Obj-C无法访问协议扩展。所以它不会被称为scrollViewWillEndDragging
从笔记
同样,唯一的例外是协议扩展。与该语言中的任何其他构造不同,协议扩展方法在虚拟分派会导致不同结果的情况下静态分派。没有编译器错误可以防止这种不匹配。 https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001707.html
正确的代码
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
print("Cell \(indexPath.row)")
cell.contentView.backgroundColor = .orange
return cell
}
//MARK: scrollview Delegate Method
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("scrollViewWillEndDragging is called")
}
}