滑塊不靈敏, 點(diǎn)擊小范圍錯(cuò)位,調(diào)整滑條高度 ,圓角 ,slider和navgation右滑返回沖突等問(wèn)題
效果圖
點(diǎn)擊,滑動(dòng),滑動(dòng)松手.gif
.h中
###這個(gè)block是用來(lái)在滑動(dòng)條變的時(shí)候所在界面ui隨之變化,本文拖動(dòng)滑動(dòng)條字體隨著變化###
typedef void(^isSliderBallMoved)(BOOL isMoved , NSInteger currentValue);
@interface GOVFontSlider : UISlider<UIGestureRecognizerDelegate>
@property (copy, nonatomic) isSliderBallMoved isSliderBallMoved;
@end
.m中
1.修改滑條寬高
bounds: 整個(gè)slider在界面的位置大小
rect :滑條的在整個(gè)slider的位置大小
@implementation GOVFontSlider
// 返回滑條的frame
- (CGRect)trackRectForBounds:(CGRect)bounds{
return CGRectMake(LAYOUT_SIZESCALE_PORTRAIT(70, 70).width, bounds.size.height /2.0 , LAYOUT_SCREENSIZE_P.width - 2 * LAYOUT_SIZESCALE_PORTRAIT(70, 70).width, 1);
}
// 返回滑塊的frame,滑塊滑動(dòng)不靈敏的話調(diào)整它的高度
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
return CGRectMake(rect.origin.x - LAYOUT_SIZESCALE_PORTRAIT(1, 1).width / 2.0 + value * (rect.size.width / 4.0), 0,LAYOUT_SIZESCALE_PORTRAIT(1, 1).width, bounds.size.height);
}
注: 滑塊的寬度設(shè)置過(guò)大導(dǎo)致點(diǎn)擊某點(diǎn),滑塊會(huì)在該點(diǎn)有小位移變化
滑塊的寬度設(shè)置過(guò)大導(dǎo)致點(diǎn)擊時(shí)小位移變化的bug.gif
#最開(kāi)始是用
[sliderView addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged]
[sliderView addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventTouchUpInside];
UITapGestureRecognizer
添加的滑動(dòng)事件和點(diǎn)擊還有滑動(dòng)松手事件,后面發(fā)現(xiàn)滑動(dòng)松手時(shí)和點(diǎn)擊事件經(jīng)常會(huì)沖突,點(diǎn)擊手勢(shì)識(shí)別的是剛觸碰slider時(shí)的那個(gè)點(diǎn)A,滑動(dòng)松手時(shí)又是另一個(gè)點(diǎn)B(我想取得是B點(diǎn),但偶爾會(huì)識(shí)別為A點(diǎn)),所以就直接用touch方法
點(diǎn)擊
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self fontTouchBeganMovedEnded:touches withEvent:event withIsFastAction:NO];
}
滑動(dòng) 松手
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self fontTouchBeganMovedEnded:touches withEvent:event withIsFastAction:YES];
}
移動(dòng)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self fontTouchBeganMovedEnded:touches withEvent:event withIsFastAction:NO];
}
第三個(gè)參數(shù),是否直接回到整點(diǎn)位置(就近取整)
- (void)fontTouchBeganMovedEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event withIsFastAction:(BOOL)isFastBack{
NSSet *allTouches = event.allTouches;
UITouch *touch = [allTouches anyObject];;
CGPoint touchPoint = [touch locationInView:self];
CGFloat value = (self.maximumValue - self.minimumValue) * ((touchPoint.x- LAYOUT_SIZESCALE_PORTRAIT(70, 70).width) / (self.frame.size.width - 2 * LAYOUT_SIZESCALE_PORTRAIT(70, 70).width));
NSInteger finalValue = round(value); #// 對(duì)value取整,回到整數(shù)位置
#// 我設(shè)置的滑條范圍是0~4,點(diǎn)擊范圍外的判斷
finalValue = finalValue < 0 ? 0: finalValue;
finalValue = finalValue > 4 ? 4 :finalValue;
[UIView animateWithDuration:0.5 animations:^{
[self setValue:isFastBack ? finalValue :value animated:YES];
}]; // 因?yàn)榧恿藙?dòng)畫(huà),在ios8,5s下會(huì)看到最右邊滑動(dòng)時(shí)小范圍變白,因?yàn)閾Q了滑動(dòng)條顏色的原因,將滑動(dòng)條顏色更改為系統(tǒng)顏色,bug消失,或者去掉動(dòng)畫(huà); 或者在互動(dòng)條下面加一條同等的線條來(lái)解決
if (self.isSliderBallMoved != nil) {
self.isSliderBallMoved(YES,finalValue); #// 用來(lái)改變界面其他的UI
}
}
@end
使用
GOVFontSlider *sliderView = [[GOVFontSlider alloc]initWithFrame: CGRectMake(0, LAYOUT_SIZESCALE_PORTRAIT(200, 200).height, LAYOUT_SCREENSIZE_P.width , LAYOUT_SIZESCALE_PORTRAIT(100, 100).height)];
[sliderView setThumbImage:[GOVCommon loadAppImage:@"settingFontSliderBall"] forState:UIControlStateNormal];
[sliderView setThumbImage:[GOVCommon loadAppImage:@"settingFontSliderBall"] forState:UIControlStateHighlighted];
sliderView.maximumValue = 4;
sliderView.minimumValue = 0;
[sliderView setValue:fontSize animated:NO];
sliderView.minimumTrackTintColor = [UIColor lightGrayColor];
圓角滑動(dòng)條
sliderView.layer.cornerRadius = imageView.frame.size.width / 2;
//將多余的部分切掉
sliderView.layer.masksToBounds = YES;
[cell.contentView addSubview:sliderView ];
sliderView.isSliderBallMoved = ^(BOOL isMoved, NSInteger currentValue) {
if (isMoved == YES) {
exampleLabel.font = FONT_SIZE_REGULAR_SCALE(fontArray[currentValue], YES);
}
};
右滑返回手勢(shì)沖突, 也可能會(huì)造成滑塊不靈敏
self.navigationController.interactivePopGestureRecognizer.enabled = NO;