iOS:如何使用滑块设置自定义背景颜色? [英] iOS: how to set custom background colour with sliders?

查看:262
本文介绍了iOS:如何使用滑块设置自定义背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想说的是,我在这个网站上看到了一些关于如何做到这一点的帖子,虽然没有一个看起来对我有用,请不要关闭,直到我工作。

First off I want to say I saw a couple of posts on this site about how to do this, although none seemed to work for me so please don't close this down until I get it working.

我想要做的是使视图的背景根据滑块的值而改变,以便用户可以选择他们想要的背景颜色。

What I want to do is make the background of the view change depending on the value of the sliders are, so that the user can choose the background colour they want.

self->colorView.backgroundColor = [UIColor myColor];
myColor = 



我想我需要一些代码,不知道如何定义我的颜色会是什么东西;像red:redSlider / 255等等其他颜色?我也不知道在哪里实现上面的代码,因为我需要它不断更新,当使用更改滑块的值。

I figure I'll need a bit of code like that, although I don't know how to define what my colour will be something; like "red: redSlider / 255" and so on for the other colours? I also don't know where to implement the code above as I need it to continuously update when the use changes the values of the sliders.

我在编程因为你可能已经拾起了,因为我只是一个少年作为一个爱好,我很欣赏简单的说明告诉我清楚我需要放置代码等。

I am quite basic at programming as you may have picked up because I'm only a teenager doing it as a hobby and I'd appreciate simple instructions telling me clearly where I need to put code etc.

ps它不会让我张贴的视图,对不起:(

p.s. It won't let me post an image of the view, sorry :(

推荐答案

在您的ViewController.h文件定义



In your ViewController.h file define

@property (nonatomic, strong) IBOutlet UISlider *mySlider;

在ViewController.m文件中,添加:

In ViewController.m file, add this:

- (void) sliderValueChanged:(UISlider *)slider
{
    // Handle your color changing logic here
    myView.backgroundColor = [UIColor colorWithRed:0.4f green:0.5f blue:1.0f alpha:1.0f];
}

在Interface Builder中,
拖动UISlider查看和设置它的值改变事件输出到 sliderValueChanged 方法。

In Interface Builder, Drag UISlider to view and set its "Value Changed" event outlet to sliderValueChanged method.

现在当你改变屏幕上的滑块,应根据您在方法中的逻辑 sliderValueChanged

Now as you change the slider on screen, the color should changed based on your logic in the method sliderValueChanged

以下是根据您的要求的逻辑:

Below is the logic as per your requirement:

- (void) sliderValueChanged:(UISlider *)slider
{
    // Assuming slider minimum is 0 and maximum is 1
    CGFloat redVal = 0.0f;
    CGFloat yellowVal = 0.0f;
    CGFloat blueVal = 0.0f;
    if (slider == redSlider)
    {
        redVal = slider.value;
    }
    else if (slider == yellowSlider)
    {
        yellowVal = slider.value;
    }
    else if (slider == blueSlider)
    {
        blueVal = slider.value;
    }
    myView.backgroundColor = [UIColor colorWithRed:redVal green:greenVal blue:blueVal alpha:1.0f];
}

这篇关于iOS:如何使用滑块设置自定义背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆