0

我想用这样的幻灯片更改 UISlider 背景颜色

在此处输入图像描述

我试过这段代码,但它不像这种类型的视图

@property (strong, nonatomic) IBOutlet UISlider *r;
@property (strong, nonatomic) IBOutlet UISlider *g;
@property (strong, nonatomic) IBOutlet UISlider *b;

-(void)blueSlider:(UISlider*)slider {
    UIColor *newColor = [UIColor colorWithRed:_r.value green:_g.value blue:_b.value alpha:1];
   _b.backgroundColor=newColor;
}

-(void)greenSlider:(UISlider*)slider {
    UIColor *newColor = [UIColor colorWithRed:_r.value green:_g.value blue:_b.value alpha:1];
    _g.backgroundColor=newColor;
}

-(void)redSlider:(UISlider*)slider  {
    UIColor *newColor = [UIColor colorWithRed:_r.value green:_g.value blue:_b.value alpha:1];
   _r.backgroundColor=newColor;
}

我想要特别的这个样子。

4

2 回答 2

2

在头文件中导入quartzCore并将图层实例定义为

#import <QuartzCore/QuartzCore.h>

@interface ViewController : UIViewController
{
    CAGradientLayer *red_gradient;
    CAGradientLayer *green_gradient;
    CAGradientLayer *blue_gradient;
}

@property (strong, nonatomic) IBOutlet UISlider *r;
@property (strong, nonatomic) IBOutlet UISlider *g;
@property (strong, nonatomic) IBOutlet UISlider *b;
@property (strong, nonatomic) IBOutlet UILabel *colorLabel;
-(IBAction)sliderValue:(UISlider*)sender;
@end

在实现文件中初始化图层并设置 UISlider 的框架,如下所示

- (void)viewDidLoad
{
    red_gradient =[CAGradientLayer layer];
    green_gradient =[CAGradientLayer layer];
    blue_gradient=[CAGradientLayer layer];

    CGRect rect=_r.frame;
    rect.size.height=10;
    [_r setFrame:rect];

    rect=_g.frame;
    rect.size.height=10;
    [_g setFrame:rect];

    rect=_b.frame;
    rect.size.height=10;
    [_b setFrame:rect];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

在视图中确实出现了方法调用 addLayers 方法

-(void)viewDidAppear:(BOOL)animated
{
    [self addLayers];
}

在值更改滑块方法调用 addSubLayers 方法为

-(IBAction)sliderValue:(UISlider*)sender
{
    [self addLayers];
    UIColor *newColor = [UIColor colorWithRed:_r.value green:_g.value blue:_b.value alpha:1];
   _colorLabel.backgroundColor = newColor;
}

最后这里是你的 addLayers 方法:---

-(void)addLayers
{
    UIColor *startColor=[UIColor colorWithRed:0 green:_g.value blue:_b.value alpha:1];
    UIColor *endColor=[UIColor colorWithRed:1 green:_g.value blue:_b.value alpha:1];

    red_gradient.frame = _r.bounds;
    red_gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];

    [red_gradient setStartPoint:CGPointMake(0.0, 0.5)];
    [red_gradient setEndPoint:CGPointMake(1.0, 0.5)];

    [_r.layer insertSublayer:red_gradient atIndex:2];

    startColor=[UIColor colorWithRed:_r.value green:0 blue:_b.value alpha:1];
    endColor=[UIColor colorWithRed:_r.value green:1 blue:_b.value alpha:1];

    green_gradient.frame = _g.bounds;
    green_gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];

    [green_gradient setStartPoint:CGPointMake(0.0, 0.5)];
    [green_gradient setEndPoint:CGPointMake(1.0, 0.5)];

    [_g.layer insertSublayer:green_gradient atIndex:2];

    startColor=[UIColor colorWithRed:_r.value green:_g.value blue:0 alpha:1];
    endColor=[UIColor colorWithRed:_r.value green:_g.value blue:1 alpha:1];

    blue_gradient.frame = _b.bounds;
    blue_gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];

    [blue_gradient setStartPoint:CGPointMake(0.0, 0.5)];
    [blue_gradient setEndPoint:CGPointMake(1.0, 0.5)];

    [_b.layer insertSublayer:blue_gradient atIndex:2];
}

它看起来像:--------

在此处输入图像描述

于 2013-07-15T16:33:24.420 回答
0

您的代码没有达到所需的外观,因为您将滑块的背景颜色全部设置在一起。

查看CGGradient 参考资料,了解如何使用渐变。

此外,您所描绘的根本不像一个滑块,而是像一个在渐变范围内的对象,它通过 a 改变值panGestureRecognizer

要了解这一点,请访问 UIPanGestureRecognizer 类参考

于 2013-07-15T13:25:24.327 回答