Is there a way to move my imageview with the move of the slider thumb?
I want to move my imageview as user slide the thumb of UISlider and the image view should also have the value of the slider. Does any one have any idea about this?? Here's the code snippets:
-(void)addSliderX:frame andminimumValue:(int)min andmaximumValue:(int)max andSliderValue:(int)value{
CGRect frame1 = CGRectFromString(frame);
statSlider = [[UISlider alloc]initWithFrame:frame1];
[statSlider setMinimumTrackImage:[[UIImage imageNamed:@"greenSlider.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:10.0] forState:UIControlStateNormal];
[statSlider setThumbImage:[UIImage imageNamed:@"sliderBar_greenThumb.png"] forState:UIControlStateNormal];
[statSlider setMinimumValue:min];
[statSlider setMaximumValue:max];
statSlider.continuous = YES;
[statSlider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
[statSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventTouchUpInside];
//bubbleViewController = [[SFNDoorBubbleViewController alloc]initWithNibName:@"SFNDoorBubbleViewController"bundle:[NSBundle mainBundle]];
//popoverController = [[UIPopoverController alloc] initWithContentViewController:bubbleViewController];
//[popoverController setPopoverContentSize:bubbleViewController.view.frame.size];
[self.view addSubview:statSlider];
}
-(void)valueChanged:(id)sender{
UISlider *localSlider = (UISlider *)sender;
bubble = [[UIView alloc]init];
back = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sliderBar_handle_number.png"]];
greenSliderValue = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
UILabel * val = [[UILabel alloc]init];
dsp =[greenSliderValue floatValue];
val.text = greenSliderValue;
[bubbleViewController updateSliderValueTo:dsp];
CGFloat sliderMax = localSlider.maximumValue;
CGFloat sliderMin = localSlider.minimumValue;
CGFloat sliderMaxMinDiff = sliderMax - sliderMin;
CGFloat sliderValue = dsp;
if(sliderMin < 0.0) {
sliderValue = dsp-sliderMin;
sliderMax = sliderMax - sliderMin;
sliderMin = 0.0;
sliderMaxMinDiff = sliderMax - sliderMin;
}
CGFloat xCoord = ((sliderValue-sliderMin)/sliderMaxMinDiff)*[localSlider frame].size.width-bubbleViewController.view.frame.size.width/2.0;
CGFloat halfMax = (sliderMax+sliderMin)/2.0;
if(sliderValue > halfMax) {
sliderValue = (sliderValue - halfMax)+(sliderMin*1.0);
sliderValue = sliderValue/halfMax;
sliderValue = sliderValue*11.0;
xCoord = xCoord - sliderValue;
}
else if(sliderValue < halfMax) {
sliderValue = (halfMax - sliderValue)+(sliderMin*1.0);
sliderValue = sliderValue/halfMax;
开发者_StackOverflow社区sliderValue = sliderValue*11.0;
xCoord = xCoord + sliderValue;
}
[self.view addSubview:bubble];
[bubble addSubview:back];
[bubble addSubview:val];
bubble.frame = CGRectMake(xCoord, 500, bubbleViewController.view.frame.size.width, bubbleViewController.view.frame.size.height);
}
Everytime changedValue:
gets called you create a new UIView, UIImageView and UILabel.
Instead of editing the old instances. That results that all old UIViews build a "path".
In your viewDidLoad you can create these three views and set on each the .tag property
viewDidLoad
UIView* bubble = [[UIView alloc]init];
UIImageView* back = [[UIImageView alloc]initWithImage:[UIImageimageNamed:@"sliderBar_handle_number.png"]];
UILabel * val = [[UILabel alloc]init]
//other configurations ...
bubble.tag = 1;
back.tag = 2;
val.tag = 3;
[self.view addSubview:bubble];
[bubble addSubview:back];
[bubble addSubview:val];
Now remove all the allocations, because we want to reuse the existing views. And we know their tags.
(void)valueChanged:(id)sender
UISlider *localSlider = (UISlider *)sender;
NSString* greenSliderValue = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
UIView *bubble = (UIView*)[self.view.subviews viewWithTag:1];
UIImageView *back = (UIImageView*)[bubble.subviews viewWithTag:2];
UILabel *val = (UILabelw*)[bubble.subviews viewWithTag:3];
dsp =[greenSliderValue floatValue];
val.text = greenSliderValue;
[bubbleViewController updateSliderValueTo:dsp];
CGFloat sliderMax = localSlider.maximumValue;
CGFloat sliderMin = localSlider.minimumValue;
CGFloat sliderMaxMinDiff = sliderMax - sliderMin;
CGFloat sliderValue = dsp;
if(sliderMin < 0.0) {
sliderValue = dsp-sliderMin;
sliderMax = sliderMax - sliderMin;
sliderMin = 0.0;
sliderMaxMinDiff = sliderMax - sliderMin;
}
CGFloat xCoord = ((sliderValue-sliderMin)/sliderMaxMinDiff)*[localSlider frame].size.width-bubbleViewController.view.frame.size.width/2.0;
CGFloat halfMax = (sliderMax+sliderMin)/2.0;
if(sliderValue > halfMax) {
sliderValue = (sliderValue - halfMax)+(sliderMin*1.0);
sliderValue = sliderValue/halfMax;
sliderValue = sliderValue*11.0;
xCoord = xCoord - sliderValue;
}
else if(sliderValue < halfMax) {
sliderValue = (halfMax - sliderValue)+(sliderMin*1.0);
sliderValue = sliderValue/halfMax;
sliderValue = sliderValue*11.0;
xCoord = xCoord + sliderValue;
}
bubble.frame = CGRectMake(xCoord, 500, bubbleViewController.view.frame.size.width, bubbleViewController.view.frame.size.height);
now you can use your existing instances
精彩评论