简体   繁体   English

自定义UISLider以在拖动拇指后更改颜色

[英]Customize UISLider to change the color after the thumb is dragged

I have this type of a custom UISlider I have implemented that looks like this: 我已经实现了这种类型的自定义UISlider ,如下所示: 在此处输入图片说明

The peculiarity of it is that you can't drag it to the left of it's initial thumb position. 它的独特之处在于您不能将其拖到初始拇指位置的左侧。

But when you drag it to the right it should change the color of the path like this leaving the color on the left as it was and colouring the path it has travelled: 但是,当您将其拖动到右侧时,它应该像这样更改路径的颜色,而将颜色保留在左侧,并为它行进的路径着色:

在此处输入图片说明

I am only aware of the two properties of UISlider - MinimumTrackImage and MaximumTrackImage that are responsible for the right and the left side of the handle. 我只知道UISlider的两个属性MinimumTrackImageMaximumTrackImage ,它们分别负责手柄的右侧和左侧。

How do I implement this strange behaviour? 如何实现这种奇怪的行为? Any ideas? 有任何想法吗? Thanks! 谢谢!

I was able to implement this with the following code. 我可以使用以下代码来实现。 The tricky part to figure out was how to set the initial image based on the starting value of the slider. 要弄清楚的棘手部分是如何根据滑块的起始值设置初始图像。 I did that by creating a 4 pixel wide image with the left 2 pixels your starting left color, and the right 2 pixels, the color you want after the drag. 我通过创建一个4像素宽的图像来做到这一点,左边的2像素是您开始的左侧颜色,右边的2像素是拖动后想要的颜色。 Basically, I stretched that image (on left only) to fill an image view, then got that image with UIGraphicsGetImageFromCurrentImageContext(). 基本上,我拉伸该图像(仅在左侧)以填充图像视图,然后使用UIGraphicsGetImageFromCurrentImageContext()获得该图像。 I then create another stretchable image with that image with stretching only on the right. 然后,我用该图像创建另一个可拉伸图像,仅在右侧拉伸。

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
@property (weak,nonatomic) IBOutlet UISlider *slider;
@property (nonatomic) CGFloat previousValue;
@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    self.previousValue = self.slider.value;
    if (self.slider.value > .01) {
        UIImage *newImage = [self imageStretchedOnLeft];
        UIImage *rightStretchImage = [newImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, newImage.size.width -2, 0, 0) resizingMode:UIImageResizingModeStretch];
        [self.slider setMinimumTrackImage:rightStretchImage forState:UIControlStateNormal];
    }else{
        [self.slider setMinimumTrackImage:[UIImage imageNamed:@"OneColor.png"] forState:UIControlStateNormal];
    }

}

-(IBAction)sliderMoved:(UISlider *)slider {
    if (slider.value < self.previousValue) {
        slider.value = self.previousValue;
    }
    self.previousValue = slider.value;
}

- (UIImage *)imageStretchedOnLeft {
    UIImage *image = [UIImage imageNamed:@"TwoColor.png"]; // 4 pixel wide 1 pixel high image. Left 2 pixels are original track color, right two are new drag color
    UIImage *cappedImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 2)];

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.slider.frame.size.width * (self.slider.value/self.slider.maximumValue), self.slider.frame.size.height)];
    iv.image = cappedImage;
    [self.view insertSubview:iv belowSubview:self.view];

    UIGraphicsBeginImageContextWithOptions(iv.frame.size, YES, [[UIScreen mainScreen] scale]);
    [iv.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [iv removeFromSuperview];

    return img;
}

I get a graphics context error if the slider's value is 0, so the if statement in the viewDidAppear method uses a different one color image if that value is less that .01. 如果滑块的值为0,则会收到图形上下文错误,因此,如果该值小于.01,则viewDidAppear方法中的if语句将使用另一幅彩色图像。

You can use this 你可以用这个

//code for slider
UIImage *minImage = [[UIImage imageNamed:@"slider_minimum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *maxImage = [[UIImage imageNamed:@"slider_maximum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *thumbImage = [UIImage imageNamed:@"thumb.png"];

[[UISlider appearance] setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[[UISlider appearance] setMinimumTrackImage:minImage forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:thumbImage forState:UIControlStateNormal];  

Here is nice tutorial of raywenderlich 这是raywenderlich的不错的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM