iphone - 如何使用幻灯片更改 UISlider 背景颜色

标签 iphone ios uislider uicolor

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

enter image description here

我试过这段代码,但它不是这种类型的 View

@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;
}

我特别想要这个造型。

最佳答案

在头文件中导入 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

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

- (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.
}

在 View 中确实出现了调用addLayers方法的方法

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

On 值更改 slider 方法调用 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];
}

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

enter image description here

关于iphone - 如何使用幻灯片更改 UISlider 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17655220/

相关文章:

iphone - 如何设置 mask 层的cornerRadius

iphone - 我想加载给定帧大小的第二个 View

iphone - 如何访问 BOOL 类型属性?

ios - 如何截取 UIView 的一部分?

javascript - iOS Cordova 应用程序 - shouldStartLoadWithRequest

iphone - IOS 4.3 如何正确解析来自 Web 服务的 Json 响应并将其绑定(bind)到 UITABLEVIEW

ios - 是否可以向 UISlider 拇指图像添加标签?

ios - 以编程方式更改 slider 值并更新标签

iOS Pandora like 底部工具栏

ios - 您不能以 root (Ionic) iOS 身份运行 CocoaPods