有没有办法通过滑块拇指的移动来移动我的图像视图?
我想在用户滑动 UISlider 的拇指时移动我的图像视图,并且图像视图也应该具有滑块的值。有人对此有任何想法吗?这是代码片段:
-(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;
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);
}
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;
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用
changedValue:
时,您都会创建一个新的 UIView、UIImageView 和 UILabel。而不是编辑旧实例。这会导致所有旧的 UIView 构建一条“路径”。
在 viewDidLoad 中,您可以创建这三个视图并在每个视图上设置
.tag 属性
viewDidLoad
现在删除所有分配,因为我们要重用现有视图。我们知道他们的标签。
(void)valueChanged:(id)sender
现在您可以使用现有实例
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
Now remove all the allocations, because we want to reuse the existing views. And we know their tags.
(void)valueChanged:(id)sender
now you can use your existing instances