gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:30:45 30 4
gpt4 key购买 nike

我修复了 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com