我有一个 UIImage
,显示在 UIImageView
中。我在 UIImageView
中还有另一张图片,它位于第一张图片上方。我希望能够仅在第一张图片的边界内拖动第二张图片。为了让我的目标更清楚一点,请看这张图片:
.
绿色图钉应该可以拖动,但不能将图钉拖到蓝色( map 之外)。 目前图钉是可拖动的,但我不知道如何检查图钉是否在 map 之外。
编辑: 我在我的 UIImageView 子类中使用了这个方法来实现可拖动的引脚:
- (UIColor *)colorAtPosition:(CGPoint)position {
CGRect sourceRect = CGRectMake(position.x, position.y, 1.f, 1.f);
CGImageRef imageRef = CGImageCreateWithImageInRect([[MapViewController sharedMapViewController]getImage].CGImage, sourceRect);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *buffer = malloc(4);
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
CGContextRef context = CGBitmapContextCreate(buffer, 1, 1, 8, 4, colorSpace, bitmapInfo);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0.f, 0.f, 1.f, 1.f), imageRef);
CGImageRelease(imageRef);
CGContextRelease(context);
CGFloat r = buffer[0] / 255.f;
CGFloat g = buffer[1] / 255.f;
CGFloat b = buffer[2] / 255.f;
CGFloat a = buffer[3] / 255.f;
free(buffer);
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
MapViewController 是 map 的 UIIImageView 所在的 Viewcontroller。所以我让这个类成为一个单例来获取 map 图像。但是我得到的颜色值又是完全有线的。我还更新了照片,因为我的用户界面略有不同。
最佳答案
只需检查拖动所在的点并使用 this 确定该点的颜色方法。
根据您的设置,您可以执行此操作,例如在 touchesMoved:
中。
关于iphone - 仅在另一个 UIImage 内部移动 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199657/