Question

I have a function rendering a circular gradient inside an ImageView and I have a panGestureRecognizer who serves as color selector.

How do I restrict the user's touch input to only be applied when his touch is within the ImageView's boundaries?

I tried this code without result :

@interface testViewController ()

@end

@implementation testViewController

- (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:)];
    [self.view addGestureRecognizer: panGesture];
    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;

    [self.view addSubview:gradientView];
    [self.view addSubview:colorView];
    gradientView.frame = CGRectMake(0,0,size.width,size.height);
}

- (void)didReceiveMemoryWarning  {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

-(void)blueSlider:(id)sender {
    bSlider = (UISlider *)sender;

    bText.text = [NSString stringWithFormat:@"%.0f",bSlider.value];
    bSlider.value = bText.text.floatValue;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

-(void)greenSlider:(id)sender  {
    gSlider = (UISlider *)sender;

    gText.text = [NSString stringWithFormat:@"%.0f",gSlider.value];
    gSlider.value = gText.text.floatValue;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

-(void)redSlider:(id)sender  {
    rSlider = (UISlider *)sender;

    rText.text = [NSString stringWithFormat:@"%.0f",rSlider.value];
    rSlider.value = rText.text.floatValue;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

-(void)saturate:(id)sender  {
    saturationSlider = (UISlider *)sender;

    UIColor *currentColor = colorView.backgroundColor;
    CGFloat hue, saturation, brightness, alpha;
    BOOL success = [currentColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    UIColor *newColor = [UIColor colorWithHue:hue saturation:saturationSlider.value brightness:hellSlider.value alpha:alphaSlider.value];

    colorView.backgroundColor = newColor;
    alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
    brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
    saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];
}

- (void)changeOpacity:(id)sender {
    alphaSlider = (UISlider *)sender;
    float red = r;
    float green = g;
    float blue = b;
    UIColor *color2 = [UIColor colorWithRed:red green:green blue:blue alpha: alphaSlider.value];
    colorView.backgroundColor = color2;
    alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
    brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
    saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];
}

- (void)changeBrightness:(id)sender {
    hellSlider = (UISlider *)sender;

    UIColor *currentColor = colorView.backgroundColor;
    CGFloat hue, saturation, brightness, alpha;
    BOOL success = [currentColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    brightness = hellSlider.value;
    UIColor *newColor = [UIColor colorWithHue:hue saturation:saturationSlider.value brightness:hellSlider.value alpha:alphaSlider.value];

    colorView.backgroundColor = newColor;
    alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
    brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
    saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];  
}

@end
Was it helpful?

Solution

Replace this line

[self.view addGestureRecognizer: panGesture];

with this

[gradientView addGestureRecognizer:panGesture];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top