This is the seventh day of my participation in the First Challenge 2022. For details: First Challenge 2022.

As a programmer, what is the most dreaded product? That’s right, the kind that thinks it should be easy for you to make. Okay, well, that’s because I’m not a good student. There’s a UI diagram,

I have not played this anyway · at that time a face meng circle, mainly is the time is very urgent. And then I solved it with this idea. It looks something like this. Here I press my this train of thought to say is how to do. And the color bar in the back, I’m going to go straight to the UI for a picture. Then the circle detractors a drag gesture and gets the color on the imgView. Each time the position of the small circle is calculated ·=-= a little helpless pain. Of course, I’m just offering a way to do it. Get the color on the view:

- (UIColor *)colorOfPoint:(CGPoint)point {
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(a);CGContextRef context = CGBitmapContextCreate(pixel, 1.1.8.4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
    
    CGContextTranslateCTM(context, -point.x, -point.y);
    
    [self.imgView.layer renderInContext:context];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
    if (pixel[0]= =0&&pixel[1]= =0&&pixel[2]= =0) {
        return [UIColor clearColor];
    }
    return color;
}
Copy the code

So I’m going to make a judgment on the color here, and I’m going to make a judgment on the color in the middle of the image that THE UI gives you, because it’s transparent in the middle, so I’m going to make a judgment on the color in the middle. Add a drag gesture to the circle:

-(void)panBtn:(UIPanGestureRecognizer *)pan
{
    CGPoint loc = [pan locationInView:self.imgView];
    CGFloat radius=W(107);
    CGFloat imgRadius=self.imgView.width/2;
    NSLog(@"--hj%@"[self colorOfPoint:loc]);
     // Check if it is on the color circle
    if (!CGColorEqualToColor([self colorOfPoint:loc].CGColor[UIColor clearColor].CGColor)) {
        CGFloat currentX=fabs(loc.x-imgRadius);
        CGFloat currentY=fabs(loc.y-imgRadius);
        CGFloat z= hypotf(currentX,currentY);
        CGFloat x=(currentX/z)*radius;
        CGFloat y=(currentY/z)*radius;
        CGFloat btnX,btnY;
        if (loc.x> =imgRadius) {
            btnX=x+imgRadius;
        }else
        {
            btnX=imgRadius-x;
        }
        if (loc.y> =imgRadius) {
            btnY=y+imgRadius;
        }else
        {
            btnY=imgRadius-y;
        }
        self.sliderBtn.center=CGPointMake(btnX, btnY);
    }
    if (pan.state = = UIGestureRecognizerStateEnded) {[self colorOfPoint:self.sliderBtn.center]; }}Copy the code

Radius this is the distance from the center of the colored circle to its center, used to calculate the center position of the small circle. Inside the algorithm is also according to what hypotenuse ah and so on, calculate the center coordinate position of the small circle, because you may drag the point is not in the center, so you adjust the center of the small circle to the correct position, this algorithm explains of very tired, their own research, is the trigonometric function. It’s a good time to read some books. Ha ha. You can then retrieve the color of the circle after the gesture, or in real time. It depends on their needs. It looks like this:

A little deviation never mind the details. This is the end, it seems not difficult, or their skills are not strong enough yo.