1

I'd like to know how I can implement a draggable object only when i touch a part that has colour. If i have an imageView with a lot of transparent padding i want the user to be able to move it when he touches the image only and not when he touches anywhere in the image view.

I am adding a gesture recognizer to the object :

UIPanGestureRecognizer *panGestureReconizer = [[UIPanGestureRecognizer alloc]      initWithTarget:self action:@selector(dragObjectWasMoved:)];
Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
Ramy Kfoury
  • 937
  • 5
  • 8
  • See this SO post: http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics – Tom Susel Apr 01 '14 at 08:42

1 Answers1

1

You can create a category of UIView or of UIImageView with the following method

-(UIColor *)colorOfPoint:(CGPoint)point
{
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.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];

    return color;
}
DigitalBrain_DEV
  • 1,093
  • 13
  • 27