将UISlider链接到颜色的不透明度?

时间:2013-07-09 06:42:02

标签: ios slider opacity

我修复了我的滑块,现在alpha正在更新。

亮度仍不起作用。

我为每个滑块创建了一个方法,并包含了必要的代码。

我将在这个答案中提供我的代码。

感谢Misha Vyrko和Wain让我朝着正确的方向前进

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
    [[UIColor whiteColor] setFill];
    UIRectFill(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height));

    int sectors = 180;
    float radius = MIN(self.view.frame.size.width, self.view.frame.size.height)/2;
    float angle = 2 * M_PI/sectors;
    UIBezierPath *bezierPath;
    for ( int i = 0; i < sectors; i++)
    {
        CGPoint center = CGPointMake((self.view.frame.size.width/2), ((self.view.frame.size.height/2)-50));
        bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:i * angle endAngle:(i + 1) * angle clockwise:YES];
        [bezierPath addLineToPoint:center];
        [bezierPath closePath];
        UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
        [color setFill];
        [color setStroke];
        [bezierPath fill];
        [bezierPath stroke];
    }
    img = UIGraphicsGetImageFromCurrentImageContext();
    iv = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:iv];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handlePan:)];

    [self.view addGestureRecognizer: panGesture];

    colorView = [[UIView alloc] init];
    CGRect bounds = self.view.bounds;
    colorView.frame = CGRectMake(CGRectGetMaxX(bounds) - 310, CGRectGetMaxY(bounds) - 50, 300, 30);
    [self.view addSubview:colorView];
    [self.view addSubview:hellSlider];
    [self.view addSubview:hellSliderLabel];
    [self.view addSubview:alphaSlider];
    [self.view addSubview:alphaSliderLabel];





}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


    - (IBAction)handlePan:(UIPanGestureRecognizer *)sender {



        CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
        if (sender.numberOfTouches)
        {

            [alphaSlider addTarget:self action:@selector(changeOpacity:) forControlEvents:UIControlEventValueChanged];
            [hellSlider addTarget:self action:@selector(changeBrightness:) forControlEvents:UIControlEventValueChanged];
            CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: sender.view];
            CGPoint center = CGPointMake((size.width/2), (size.height /2)-50);
            CGPoint delta = CGPointMake(lastPoint.x - center.x,  lastPoint.y - center.y);
            CGFloat angle = (delta.y == 0 ? delta.x >= 0 ? 0 : M_PI : atan2(delta.y, delta.x));
            angle = fmod(angle,  M_PI * 2.0);
            angle += angle >= 0 ? 0 : M_PI * 2.0;
            UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:1. brightness:hellSlider.value alpha:alphaSlider.value];

            if ([color getRed: &r green: &g blue:&b alpha: &a])
            {
                NSLog(@"Color value - R : %g G : %g : B %g", r*255, g*255, b*255);
            }
            float red = r;            
            float green = g;          
            float blue = b;
            float alp = alphaSlider.value;
            UIColor *color2 = [UIColor colorWithRed:red green:green blue:blue alpha: alp];
            colorView.backgroundColor = color2;
        }

    }

- (void)changeOpacity:(id)sender {
    alphaSlider = (UISlider *)sender;
     UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:1. brightness:hellSlider.value alpha:alphaSlider.value];
    [UIColor colorWithRed:r green:g blue:b alpha:alphaSlider.value];

    if ([color getRed: &r green: &g blue:&b alpha: &a])
    {
        NSLog(@"Color value - R : %g G : %g : B %g", r*255, g*255, b*255);
    }
    float red = r;
    float green = g;
    float blue = b;
    float alp = alphaSlider.value;
    UIColor *color2 = [UIColor colorWithRed:red green:green blue:blue alpha: alp];
    colorView.backgroundColor = color2;
}

- (void)changeBrightness:(id)sender {
    hellSlider = (UISlider *)sender;
    UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:1. brightness:hellSlider.value alpha:alphaSlider.value];
    if ([color getRed: &r green: &g blue:&b alpha: &a])
    {
        NSLog(@"Color value - R : %g G : %g : B %g", r*255, g*255, b*255);
    }
    float red = r;
    float green = g;
    float blue = b;
    float alp = alphaSlider.value;
    UIColor *color2 = [UIColor colorWithRed:red green:green blue:blue alpha: alp];
    colorView.backgroundColor = color2;
}

1 个答案:

答案 0 :(得分:2)

UISlider:UIControl =&gt;你可以

[mySlider addTarget:self action:@selector(changeOpacity:) forControlEvents:UIControlEventValueChanged];

...

- (void)changeOpacity:(id)sender {
    UISlider *mySlider = (UISlider *)sender;
   [UIColor colorWithRed:x green:y blue:z alpha:mySlider.value];
}