0

スライダーを修正し、現在アルファがライブで更新されています。

明るさはまだ機能していません。

スライダーごとにメソッドを作成し、必要なコードを含めました。

この回答でコードを提供します。

私を正しい方向に導いてくれた 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;
}
4

1 に答える 1