Objective--C Slider实现三原色之间颜色变化


// 定义三个属性,分别为红,绿,蓝的slider

@property(nonatomic,retain)UISlider *redSlider;

@property(nonatomic,retain)UISlider *greenSlider;

@property(nonatomic,retain)UISlider *blueSlider;


// 只要设置了属性,就要进行dealloc

- (void)dealloc

{

    [_redSlider release];

    [_blueSlider release];

    [_greenSlider release];

    [super dealloc];

}




在viewDidLoad方法下

self.redSlider = [[UISlider alloc] initWithFrame:CGRectMake(80, 100, 250, 100)];

    // 最小值方向滑动线的颜色

    self.redSlider.minimumTrackTintColor = [UIColor blueColor];

    self.redSlider.minimumValue = 0;

    // 最大值方向滑动线的颜色

    self.redSlider.maximumTrackTintColor = [UIColor magentaColor];

    self.redSlider.maximumValue = 1;

    // 滑动球的颜色

    self.redSlider.thumbTintColor = [UIColor brownColor];

    [self.view addSubview:self.redSlider];

    // 设置点击时间

    [self.redSlider addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];

    [self.redSlider release];



// 用同样方法设置绿色和蓝色

self.blueSlider = [[UISlider alloc] initWithFrame:CGRectMake(80, 200, 250, 100)];

    self.blueSlider.minimumTrackTintColor = [UIColor blueColor];

    self.blueSlider.minimumValue = 0;

    self.blueSlider.maximumTrackTintColor = [UIColor magentaColor];

    self.blueSlider.maximumValue = 1;

    self.blueSlider.thumbTintColor = [UIColor brownColor];

    [self.blueSlider addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:self.blueSlider];

    [self.blueSlider release];

    

    

    self.greenSlider = [[UISlider alloc] initWithFrame:CGRectMake(80, 300, 250, 100)];

    self.greenSlider.minimumTrackTintColor = [UIColor blueColor];

    self.greenSlider.minimumValue = 0;

    self.greenSlider.maximumTrackTintColor = [UIColor magentaColor];

    self.greenSlider.maximumValue = 1;

    self.greenSlider.thumbTintColor = [UIColor brownColor];

    [self.greenSlider addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:self.greenSlider];

    [self.greenSlider release];





// 点击方法实现

- (void)click:(UISlider *)slider{


    self.view.backgroundColor = [[UIColor alloc] initWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:1];

    

}








你可能感兴趣的:(Objective-C)