iOS 中scrollView整个页面滑动自动居中。
在iOS中,如果整体的宽度大于单个item的宽度,自动滑动居中则不好用,解决方法如下。宽度还是设置成item的宽度。在 HFScrollViewSuperView上面addsubView。实现原理看代码就能明白。
HFScrollViewSuperView
#import <UIKit/UIKit.h>
@interface HFScrollViewSuperView : UIView
@property (nonatomic, strong) UIScrollView *scrollView;
@end
#import "HFScrollViewSuperView.h"
@implementation HFScrollViewSuperView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
if ([hitTestView isKindOfClass:[UIButton class]]) {
return hitTestView;
}
}
return self.scrollView;
}
return nil;
}
@end