有没有办法通过滑块拇指的移动来移动我的图像视图?

发布于 2024-10-31 18:52:38 字数 3057 浏览 5 评论 0原文

我想在用户滑动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-11-07 18:52:38

每次调用 changedValue: 时,您都会创建一个新的 UIView、UIImageView 和 UILabel。
而不是编辑旧实例。这会导致所有旧的 UIView 构建一条“路径”。

在 viewDidLoad 中,您可以创建这三个视图并在每个视图上设置 .tag 属性
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];

现在删除所有分配,因为我们要重用现有视图。我们知道他们的标签。
(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);

现在您可以使用现有实例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文