需求:做一个滑动价格选择器
这边主要用到的控件:
1、两个ImageView
2、一个显示灰色的View
3、一个显示中间区域的View
主要代码展示:
-(void)leftHandMove:(UIPanGestureRecognizer*)pan{
CGPointpoint = [pantranslationInView:self.leftImageView];
CGFloatx =self.leftImageView.center.x+ point.x;
if(x >=0&& x<=self.rightImageView.center.x- (self.frame.size.width*self.mixPrice/self.maxPrice)) {
[selfaddSubview:self.leftImageView];
self.leftImageView.center = CGPointMake(x, self.leftImageView.center.y);
[pansetTranslation:CGPointZero inView:self];
[self judgecontentPointCenterB];
}elseif(x <=0){
[selfaddSubview:self.leftImageView];
self.leftImageView.center = CGPointMake(0, self.leftImageView.center.y);
[pansetTranslation:CGPointZero inView:self];
[self judgecontentPointCenterB];
}else if (x >= self.rightImageView.center.x - (self.frame.size.width*self.mixPrice/self.maxPrice)){
[selfaddSubview:self.leftImageView];
self.leftImageView.center = CGPointMake(self.rightImageView.center.x - (self.frame.size.width*self.mixPrice/self.maxPrice), self.leftImageView.center.y);
[pansetTranslation:CGPointZero inView:self];
[self judgecontentPointCenterB];
}
}
-(void)rightHandMove:(UIPanGestureRecognizer*)pan{
CGPointpoint = [pantranslationInView:self.rightImageView];
CGFloatx =self.rightImageView.center.x+ point.x;
if(x >=self.leftImageView.center.x+self.frame.size.width*self.mixPrice/self.maxPrice&& x<=self.frame.size.width) {
[self addSubview:self.rightImageView];
self.rightImageView.center = CGPointMake(x, self.rightImageView.center.y);
[pansetTranslation:CGPointZero inView:self];
[self judgecontentPointCenterB];
}else if (x <= self.leftImageView.center.x + self.frame.size.width*self.mixPrice/self.maxPrice){
[self addSubview:self.rightImageView];
self.rightImageView.center = CGPointMake(self.leftImageView.center.x + self.frame.size.width*self.mixPrice/self.maxPrice, self.rightImageView.center.y);
[pansetTranslation:CGPointZero inView:self];
[self judgecontentPointCenterB];
}elseif(x >=self.frame.size.width){
[self addSubview:self.rightImageView];
self.rightImageView.center = CGPointMake(self.frame.size.width, self.rightImageView.center.y);
[pansetTranslation:CGPointZero inView:self];
[self judgecontentPointCenterB];
}
}
主要根据滑动的进度来计算位置,供大家参考
如果有问题请联系我邮箱:578145120@qq.com或者添加我的微信:chuan88zl