效果:
image.png
源代码
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SliderView : UIView
//刷新
-(void)SliderViewRefreshingDidScroll:(UIScrollView *)scrollView;
-(instancetype)initWithFrame:(CGRect)frame ScrollView:(UIScrollView *)scrollView;
@end
#import "SliderView.h"
@interface SliderView()<UIGestureRecognizerDelegate>
@property (strong, nonatomic)UIPanGestureRecognizer *panGestureRecognizer ;
@property (strong, nonatomic) UIScrollView * scrollView;
@end
@implementation SliderView
-(SliderView *)initWithFrame:(CGRect)frame ScrollView:(UIScrollView *)scrollView
{
if (self = [super initWithFrame:frame]) {
self.scrollView = scrollView;
CGFloat safeAreaTop = 0;
if (@available(iOS 11.0, *)) {
safeAreaTop = self.scrollView.safeAreaInsets.top;
self.scrollView.showsVerticalScrollIndicator = NO;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y+safeAreaTop, self.width, self.height);
}
[self creatSubViews];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self creatSubViews];
}
return self;
}
-(void)creatSubViews
{
self.backgroundColor = [UIColor clearColor];
UIImageView * imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"huakuai"];
[self addSubview:imageView];
imageView.userInteractionEnabled = NO;
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_offset(0);
make.centerX.mas_offset(0);
}];
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
self.panGestureRecognizer.delegate = self;
[self addGestureRecognizer: self.panGestureRecognizer];
}
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
{
// 手指滑动坐标
CGPoint translation = [recognizer translationInView:self.superview];
//Y轴位置
CGFloat Y = recognizer.view.center.y + translation.y;
CGFloat height = self.scrollView.height-self.frame.size.height;
CGFloat safeAreaBottom = 0 ;
CGFloat safeAreaTop = 0 ;
//ios 11 适配安全距离,安全距离区域不能滑动
if (@available(iOS 11.0, *)) {
safeAreaBottom = self.scrollView.safeAreaInsets.bottom;
safeAreaTop = self.scrollView.safeAreaInsets.top;
height = height - safeAreaBottom-safeAreaTop;
}
// 超出最小范围
// 中心Y+self.frame.size.height/2+safeAreaTop = Y
if ((Y-self.frame.size.height/2-safeAreaTop)<0) {
//safeAreaTop+self.frame.size.height/2 起始位置
recognizer.view.centerY = safeAreaTop+self.frame.size.height/2;
[recognizer setTranslation:CGPointZero inView:self.superview];
[self SliderViewPanDidScroll];
}else if(Y-self.frame.size.height/2-safeAreaTop<height) {//可以滑动区域
recognizer.view.centerY = Y;
[recognizer setTranslation:CGPointZero inView:self.superview];
[self SliderViewPanDidScroll];
}
else{//超出最大范围
// height+self.frame.size.height/2+safeAreaTop 终点位置
recognizer.view.centerY = height+self.frame.size.height/2+safeAreaTop;
[recognizer setTranslation:CGPointZero inView:self.superview];
[self SliderViewPanDidScroll];
}
}
-(void)SliderViewPanDidScroll
{
if (!self.scrollView) {
return;
}
//scrollView 内容高度-安全距离 = 可以偏移的高度
CGFloat allHeight = self.scrollView.contentSize.height;
//滑动的高度
CGFloat sliderH = self.frame.size.height;
// 滑块可以滑动的高度
CGFloat height = self.scrollView.height-sliderH;
//
CGFloat safeAreaBottom = 0 ;
CGFloat safeAreaTop = 0;
if (@available(iOS 11.0, *)) {
safeAreaBottom = self.scrollView.safeAreaInsets.bottom;
safeAreaTop = self.scrollView.safeAreaInsets.top;
height = height - safeAreaBottom - safeAreaTop;
allHeight = allHeight - safeAreaBottom ;
}
CGFloat MaxOffSetY = self.scrollView.contentSize.height-self.scrollView.height+safeAreaBottom;
CGFloat offsetY = (self.frame.origin.y-safeAreaTop-sliderH/2)/height*allHeight+safeAreaTop;
CGFloat minOffSet = -safeAreaTop;
if (offsetY>MaxOffSetY) {
offsetY = MaxOffSetY;
}
if (offsetY<minOffSet) {
offsetY = minOffSet;
}
[self.scrollView setContentOffset:CGPointMake(0, offsetY)];
}
-(void)SliderViewRefreshingDidScroll:(UIScrollView *)scrollView
{
if ( self.panGestureRecognizer.state!=UIGestureRecognizerStatePossible ) {
return;
}
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat safeAreaBottom = 0 ;
CGFloat safeAreaTop = 0 ;
CGFloat sliderH = self.frame.size.height;
CGFloat allHeight = scrollView.contentSize.height;
CGFloat height = scrollView.height-sliderH;
if (@available(iOS 11.0, *)) {
safeAreaBottom = scrollView.safeAreaInsets.bottom;
safeAreaTop = scrollView.safeAreaInsets.top;
height = height - safeAreaBottom-safeAreaTop;
}
CGFloat Y = offsetY/allHeight * height+safeAreaTop;
if (offsetY <= 0) {
self.frame = CGRectMake(self.frame.origin.x, Y, self.width, sliderH);
}else{
if (Y<height) {
self.frame = CGRectMake(self.frame.origin.x, Y, self.width, sliderH);
}else{
self.frame = CGRectMake(self.frame.origin.x,height+safeAreaTop, self.width, sliderH);
}
}
}
@end
使用
-(SliderView *)sliderView
{
if (!_sliderView) {
_sliderView = [[SliderView alloc] initWithFrame:CGRectMake(ScreenWidth/3.0*2-15, 0, 15, 30) ScrollView:self.tableView];
}
return _sliderView;
}
@end
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.sliderView SliderViewRefreshingDidScroll:scrollView];
}
网友评论