gpt4 book ai didi

ios - 在当前触摸位置添加指示器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:46 26 4
gpt4 key购买 nike

我正在开发颜色选择器并使用圆形渐变和 panGesture 来选择颜色。

我可以在当前触摸位置添加十字准线或其他东西吗?

我需要十字准线可见,但它不应干扰渐变。

这是添加渐变的代码:

- (void)viewDidLoad
{
[super viewDidLoad];





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


int sectors = 180;
float radius = MIN(size.width, size.height)/2;
float angle = 2 * M_PI/sectors;
UIBezierPath *bezierPath;
for ( int i = 0; i < sectors; i++)
{
CGPoint center = CGPointMake(((size.width)/2), ((size.height)/2));
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();
gradientView = [[UIImageView alloc]initWithImage:img];;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

colorView = [[UIView alloc] init];
colorView.frame = CGRectMake(0, 00, 50, 50);


rText.textAlignment = NSTextAlignmentCenter;
gText.textAlignment = NSTextAlignmentCenter;
bText.textAlignment = NSTextAlignmentCenter;
saturationText.textAlignment = NSTextAlignmentCenter;
alphaText.textAlignment = NSTextAlignmentCenter;
brightnessText.textAlignment = NSTextAlignmentCenter;

rText.keyboardType = UIKeyboardTypeNumberPad;
gText.keyboardType = UIKeyboardTypeNumberPad;
bText.keyboardType = UIKeyboardTypeNumberPad;

saturationText.keyboardType = UIKeyboardTypeNumberPad;
alphaText.keyboardType = UIKeyboardTypeNumberPad;
brightnessText.keyboardType = UIKeyboardTypeNumberPad;



gradientView.frame = CGRectMake(0,0,size.width,size.height);
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:gradientView];
gradientView.userInteractionEnabled = YES;
[gradientView addGestureRecognizer: panGesture];

[self.view addSubview:colorView];





}

handlePan 方法:

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


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

float radius = MIN(size.width, size.height)/2;

[alphaSlider addTarget:self action:@selector(changeOpacity:) forControlEvents:UIControlEventValueChanged];
[hellSlider addTarget:self action:@selector(changeBrightness:) forControlEvents:UIControlEventValueChanged];
[saturationSlider addTarget:self action:@selector(saturate:) forControlEvents:UIControlEventValueChanged];
[rSlider addTarget:self action:@selector(redSlider:) forControlEvents:UIControlEventValueChanged];
[gSlider addTarget:self action:@selector(greenSlider:) forControlEvents:UIControlEventValueChanged];
[bSlider addTarget:self action:@selector(blueSlider:) forControlEvents:UIControlEventValueChanged];




CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: gradientView];
CGPoint center = CGPointMake((size.width/2), (size.height /2));
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;
if((lastPoint.x - center.x) + (lastPoint.y - center.y)/2 < radius)
{
UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:saturationSlider.value 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;
rText.text = [NSString stringWithFormat:@"%.0f",rSlider.value];
gText.text = [NSString stringWithFormat:@"%.0f",green*255];
bText.text = [NSString stringWithFormat:@"%.0f",blue*255];


colorView.backgroundColor = [UIColor colorWithRed:(rText.text.floatValue/255) green:(gText.text.floatValue/255) blue:(bText.text.floatValue/255) alpha:alphaSlider.value];
rSlider.value = red*255;
gSlider.value = green*255;
bSlider.value = blue*255;
alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];

}





}
}

问题已解决,谢谢。

最佳答案

好吧,您绝对“可以”在当前触摸位置添加十字准线。

我可以告诉您其中一种可行的方法。

实现

 - (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer

Action 处理器。

将十字准线的图像加载到 UIImage(比如 overlayImage)中:

  overlayImage =[UIImage imageNamed:@"crosshair.png"];

并将其设置为 UIView(例如 overlayView)。[这可以在您的 viewDidLoad 方法中完成。]

现在,将以下代码添加到您的 handlePan 方法中:

CGPoint translation = [recognizer translationInView:colorView]; //whichever view you want.
_overlayView.center = CGPointMake(_overlayView.center.x + translation.x,
_overlayView.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:colorView];

if (recognizer.state == UIGestureRecognizerStateEnded) {

CGPoint velocity = [recognizer velocityInView:colorView];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 800;
// NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(_overlayView.center.x + (velocity.x * slideFactor),
_overlayView.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), colorView.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), colorView.bounds.size.height);

[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
_overlayView.center = finalPoint;
} completion:nil];

这应该可以解决问题。

您也可以通过实现 touchesBegan 和 touchesMoved 方法来尝试这样做。

关于ios - 在当前触摸位置添加指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17651540/

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