Moving a UIImage only inside another UIImage

I have a UIImage that is shown in a UIImageView . I also have another image in the UIImageView that lies above the first image. I want to be able to drag the second image only within the borders of the first image. To make my goal clearer, look at this image: .

The green pin should be draggable, but it should not be possible to drag the pin to blue (outside the map). You can drag this icon right now, but I don’t know how to check if this icon is outside the map.

UPDATE: I used this method in my UIImageView subclass for drag and drop pins:

 - (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 is the Viewcontroller where the UIIImageView for the map is located. So I made this class a singleton to get a map image. But again, the meanings that I get for the color are completely related. I also updated the photo because my user interface has become a bit different.

+3
source share
5 answers

Have you tried using the UIResponder touch methods in your custom UIViewController and then referencing 2 UIViews as follows?

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* t = [touches anyObject]; if (t.tapCount == 1 && [yourPinView pointInside:pt withEvent:nil]) { CGPoint pt = [t locationInView:yourMapView]; if ([self getColorOfPt:pt] != <blue>) { state = MOVING; } } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (MOVING == state) { UITouch* t = [touches anyObject]; // Move the pin only if the current point color is not blue. if ([self getColorOfPt:pt] != <blue>) { CGRect r = yourPinView.frame; r.origin.x = <new point based on diff> r.origin.y = <new point based on diff> yourPinView.frame = r; } } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* t = [touches anyObject]; if (t.tapCount == 1 && MOVING == state) { state == NOT_MOVING; } } 
+4
source

Just check where the drag is located and determine the color at that point using this method.

Depending on your setup, you can do this, for example. in touchesMoved:

+5
source

You can try picking a color using CGPoint UIImage : iPhone Objective C: How to get the pixel color of the affected point in UIImageView? and determine if it is "blue."
And if it's blue, you don’t need to (re) position the contact

+3
source

You can use the answers to these questions to get the color of the pixel.

Get pixel color UIImage

How to get RGB values ​​for a pixel in an image on iphone

I also found one beautiful tutorial: What color is my pixel? IPhone color picker

Then determine if there is a Blue color . And if it's Blue , then @basvk said don't just flip the pin upside down. Hope you get something from this.

+1
source

Just giving you an idea.

I hope the map is an SVG file, or if its jpeg can be easily converted to SVG using Adobe Illustrator. Or there are many other options for this.

and here you can find how to hide svg on the way.

here you can find how to convert EPS (illustrator paths) to CGPath

then you can check that the touch point is in the way

 if([CGPathContainsPoint:myPoint]){ } else{ } 
+1
source

All Articles