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

标签 ios slider opacity

我修复了 slider ,现在 alpha 正在实时更新。

亮度还是不行。

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

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

感谢 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;
}

最佳答案

UISlider : UIControl => 你可以

[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];
}

关于ios - 将 UISlider 链接到颜色的不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541640/

相关文章:

html - 在 HTML/CSS 中定位?

css - 范围输入拇指随时间偏移

user-interface - iOS - 匹配界面颜色

ios - 如何检测 SKSpriteNode 和 SKView SpriteKit 之间的碰撞

c# - 带有 slider 绑定(bind)的 TranslateTransform 不起作用

css - Chrome 无法正确呈现纯 css 覆盖

html - HTML/CSS 中的部分图像不透明度

c# - Web 浏览器中的透明内容

ios - MFMailComposeViewController 发送按钮禁用

ios - 为什么我的 UIAlertController 中的选项变暗,我该如何解决?