简体   繁体   English

滑动时更改UIslider拇指图像

[英]Change UIslider thumb image while sliding

I want to change UISlider thumb image during the slide. 我想在幻灯片中更改UISlider拇指图像。
Basically to set thumbImage acording to the value. 基本上根据值设置thumbImage。
在此输入图像描述

The idea was to rotate the image according to the value and set it to the thumb. 我们的想法是根据值旋转图像并将其设置为拇指。
So I tried to set the thumb image by overriding 所以我尝试通过覆盖来设置拇指图像

-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
 [self setThumbImage:[self imageRotatedByDegrees:50] forState:UIControlStateHighlighted];

and I also tried the same thing when I added an action to my slider. 当我向滑块添加动作时,我也尝试了同样的事情。 Unfortunately in both implementations the image simply disappears. 不幸的是,在两种实现中,图像都消失了。
Do you think it possible to achieve in the way I do it? 你觉得有可能以我的方式实现吗? If NO please explain and suggest an alternative way (hopefully not one that will customize and replace the whole slider) 如果没有请解释并建议一种替代方式(希望不是一个可以自定义和替换整个滑块的方法)
If Yes I will really appreciate code sample. 如果是,我将非常感谢代码示例。 The more close answer that I found here 我在这里找到的答案越接近

Thanks a lot. 非常感谢。

I found a mistake in my Code. 我在我的代码中发现了一个错误。 Apparently I was releasing one of the image before calling CGContextDrawImage . 显然我在调用CGContextDrawImage之前发布了一个图像。 (I still need to improve my GUI appearance to make it more nice, for instance to make the track image as I planed I made the original one transparent and added the as subview the one I need.) (我仍然需要改进我的GUI外观以使其更好,例如制作轨道图像,因为我计划我将原始图像透明并添加为我需要的子视图。)

- (id)initWithFrame:(CGRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    self.thumbImage = [self imageWithImage:[UIImage imageNamed:@"IconForSlider.png"] scaledToSize:CGSizeMake(frame.size.height, frame.size.height)];
    size = self.thumbImage.size.height;
    [self setThumbImage:[self imageRotatedByDegrees:0] forState:UIControlStateNormal];
    [self addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventValueChanged];

}
return self;
}

-(void)changeValue{
NSLog(@"ChangeValue");
[self setThumbImage:(UIImage *)[self imageRotatedByDegrees:(self.value * 100*(10/9))] forState:UIControlStateHighlighted];
[self setThumbImage:(UIImage *)[self imageRotatedByDegrees:(self.value * 100*(10/9))] forState:UIControlStateNormal];
 }

#pragma mark ImageResize
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
 }

 #pragma mark Rotate

- (UIImage *)imageRotatedByDegrees:(float)degrees
 {
NSLog(@"ImageRotateByDegres");
static float Prc = 0.6;
degrees = (degrees > 90)? 90: degrees;

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,size,size)];
CGSize rotatedSize = rotatedViewBox.frame.size;

 // Create the bitmap context

UIGraphicsBeginImageContextWithOptions(rotatedSize , NO, 0.0);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
UIColor *color = [self.colorsArray objectAtIndex:lround(self.value*100)];
[color setFill];
CGContextRef bitmap = UIGraphicsGetCurrentContext();

[[UIColor colorWithRed:((139 + (116/100)*self.value*100)/255.0) green:141/255.0f blue:149/255.0f alpha:1.0f] setFill];
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

// Rotate the image context
CGContextRotateCTM(bitmap,DEGREES_TO_RADIANS(degrees));

CGContextClipToMask(bitmap, CGRectMake(-1*size/ 2, -1*size / 2, size, size), [self.thumbImage CGImage]);
CGContextAddRect(bitmap, CGRectMake(-1*size/ 2, -1*size / 2, size, size));
CGContextDrawPath(bitmap,kCGPathFill);
 //Prc and 1.07 are for better view
CGContextDrawImage(bitmap, CGRectMake(-1.07*size/2*Prc, -1*size/2*Prc,size*Prc,size*Prc), [[UIImage imageNamed:@"ActiveIcon.png"]CGImage]);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

 }

with some help from this link I create an array of colours for my slider Get Pixel Color of Uiimage 在这个链接的帮助下,我为我的滑块获取了Uiimage的Pixel Color的颜色数组

- (UIColor *)GetColorAtValue:(float)value{

long pixelInfo = (long)floorf(_sliderImage.size.width  * _sliderImage.size.height/2 + _sliderImage.size.width * value) * 4  ;

UInt8 red = data[pixelInfo];         // If you need this info, enable it
UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it
UInt8 blue = data[pixelInfo + 2];    // If you need this info, enable it
//UInt8 alpha = data[pixelInfo + 3];     // I need only this info for my maze game
//CFRelease(pixelData);

UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1]; // The pixel color info

return color;

} }

在此输入图像描述

在此输入图像描述

在此输入图像描述

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

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