由一个滑动条的任务需求产生一个对UISlider控件的探讨
任务需求样式:
思路:自定义UISlider
动手吧, Boy!
@interface NoviceBidView : UIView
@property (nonatomic, strong) UIView *left;
@property (nonatomic, strong) UIView *right;
@property (nonatomic, strong) UISlider *slider;
@end
_slider = [[UISlider alloc] initWithFrame:CGRectMake(30.0, self.pieChart.center.y + 80 + 30 - 5, [[UIScreen mainScreen] bounds].size.width - 60.0, 45)];
[_slider setThumbImage:[self OriginImage:[UIImage imageNamed:@"icon_hand.png"] scaleToSize:CGSizeMake(50, 50)] forState:UIControlStateNormal];
_slider.minimumTrackTintColor = [UIColor clearColor];
_slider.maximumTrackTintColor = [UIColor clearColor];
[_slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[_slider addTarget:self action:@selector(sliderDragUp:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_slider];
- (void)sliderValueChanged:(UISlider *)slider
{
if (slider.value < 0.5) {
_left.frame = CGRectMake(40, self.pieChart.center.y + 80 + 30 - 15, (kScreenWidth - 80) * slider.value + 40, 40);
_right.frame = CGRectMake(40 + (kScreenWidth - 80) * slider.value, self.pieChart.center.y + 80 + 30 - 15, (kScreenWidth - 80) * (1 - slider.value), 40);
}else if(slider.value>0.9){
_left.frame = CGRectMake(40, self.pieChart.center.y + 80 + 30 - 15, (kScreenWidth - 80) * slider.value, 40);
_right.frame = CGRectMake((kScreenWidth - 80) * slider.value + 10, self.pieChart.center.y + 80 + 30 - 15, (kScreenWidth - 80) * (1 - slider.value) + 30, 40);
}else{
_left.frame = CGRectMake(40, self.pieChart.center.y + 80 + 30 - 15, (kScreenWidth - 80) * slider.value + 20, 40);
_right.frame = CGRectMake(20 + (kScreenWidth - 80) * slider.value, self.pieChart.center.y + 80 + 30 - 15, (kScreenWidth - 80) * (1 - slider.value) + 20, 40);
}
}
- (void)sliderDragUp:(UISlider *)slider
{
if (slider.value < 0.9) {
slider.value = 0;
}else
{
slider.value = 1;
[[NSNotificationCenter defaultCenter] postNotificationName:@"fastBit" object:nil];
[self performSelector:@selector(changeToZero) withObject:nil afterDelay:1];
}
_left.frame = CGRectMake(40, self.pieChart.center.y + 80 + 30 - 15, ([[UIScreen mainScreen] bounds].size.width-80.0) * slider.value, 40);
_right.frame = CGRectMake(40 + ([[UIScreen mainScreen] bounds].size.width-80.0) * slider.value, self.pieChart.center.y + 80 + 30 - 15, ([[UIScreen mainScreen] bounds].size.width-80.0) * (1 - slider.value), 40);
}
- (void)changeToZero
{
_slider.value = 0;
_left.frame = CGRectMake(40.0, self.pieChart.center.y + 80 + 55 - 5, 40, 45);
_right.frame = CGRectMake(40.0, self.pieChart.center.y + 80 + 55 - 5, [[UIScreen mainScreen] bounds].size.width - 80.0, 45);
}
-(UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size//调整图片尺寸
{
UIGraphicsBeginImageContext(size);//size为CGSize类型,即你所需要的图片尺寸
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
上述方法和属性已经基本覆盖了UISlider的API中定义的常规使用方法了.
如果您觉得博文对您有帮助, 希望您能打发给博主一点茶水钱, 您的支持就是博主最大的动力!