仅在触摸具有颜色的零件时拖动对象或图像视图



我想知道如何仅在触摸具有颜色的部分时才实现可拖动的对象。如果我有一个带有大量透明填充的imageView,我希望用户能够在仅在触摸图像时移动它,而不是在触摸图像视图中的任何地方时移动它。

我正在向对象添加一个手势识别器:

UIPanGestureRecognizer *panGestureReconizer = [[UIPanGestureRecognizer alloc]      initWithTarget:self action:@selector(dragObjectWasMoved:)];
您可以使用

以下方法创建UIView或UIImageView的类别

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

最新更新