简体   繁体   中英

Limiting a pan gesture recognizer to an ImageView?

I have an ImageView containing a circular gradient and a panGestureRecognizer that I want to limit specifically to the gradient to prevent interference with the UISliders in the view or the white background.

How do I only recognize touches within the gradient?

As of now, I am using this equation with bad results :

        CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: gradientView];
        CGPoint center = CGPointMake((size.width/2), (size.height /2));

These variables are used to indicate the center of the gradient. This is the equation :

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

        }

This code above is inside my panGestureRecognizer 's method.

You can add another UIView on the top of that circular view and apply gesture to that View.

UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[gestureView.layer setCornerRadius:gestureView.frame.size.width/2];

This will give you a circular UIView . Adjust the size and the position according to your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM