这是一个 UIScrollView 子类方法,它维护单元格选择功能,还允许在滚动视图内外进行 UIControl 选择(按钮等)。
斯威夫特 3
class CellContentScrollView: UIScrollView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitTargetView = super.hitTest(point, with: event)
return hitTargetView as? UIControl ?? (hitTargetView == self ? nil : superview)
}
override func didMoveToSuperview() {
superview?.addGestureRecognizer(panGestureRecognizer)
}
}
斯威夫特 2
class CellContentScrollView: UIScrollView {
// MARK: - UIView override
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let hitTargetView = super.hitTest(point, withEvent: event)
return hitTargetView as? UIControl ?? (hitTargetView == self ? nil : superview)
}
override func didMoveToSuperview() {
superview?.addGestureRecognizer(panGestureRecognizer)
}
}
Objective-C
@implementation CellContentScrollView
#pragma mark - UIView override
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitTargetView = [super hitTest:point withEvent:event];
if ([hitTargetView isKindOfClass:UIControl.class]) {
return hitTargetView;
} else if (hitTargetView != self) {
return self.superview;
}
return nil;
}
- (void)didMoveToSuperview {
[self.superview addGestureRecognizer:self.panGestureRecognizer];
}
@end