簡體   English   中英

僅在另一個UIImage中移動UIImage

[英]Move UIImage only inside of another UIImage

我有一個UIImage ,顯示在UIImageView 我還在UIImageView有另一個圖像,它位於第一個圖像之上。 我希望能夠僅在第一個圖像的邊框內拖動第二個圖像。 為了讓我的目標更清晰一點,看看這張圖片:

綠色引腳應該是可拖動的,但不應該將引腳拖動到藍色(地圖外部)。 此刻引腳是可拖動的,但我不知道如何檢查引腳是否在地圖之外。

編輯:我在我的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是Viewcontroller,其中地圖的UIIImageView是。 所以我讓這個類成為一個單例來獲取地圖圖像。 但同樣,我得到的顏色值是完全有線的。 我也更新了照片,因為我的ui略有不同。

只需檢查拖動的位置,然后使用方法確定該點的顏色

根據您的設置,您可以執行此操作,例如touchesMoved: .

您是否嘗試在自定義UIViewController中實現UIResponder的觸摸方法,然后按如下方式引用2 UIViews?

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

您可以嘗試在UIImageCGPoint處選擇顏色的方法: iPhone目標C:如何在UIImageView上獲取觸摸點的像素顏色? 並檢測它是否是“藍色”。
如果它是藍色,則不要(重新)定位銷

您可以使用此問題的答案來獲取像素顏色。

獲取UIImage的Pixel顏色

如何獲取iphone上圖像上像素的RGB值

我還找到了一個漂亮的教程: 我的像素是什么顏色的? iPhone上基於圖像的顏色選擇器

然后檢測它是否是藍色 如果它是藍色,那么@basvk說不要(重新)定位引腳。 希望你從中得到一些東西。

我只是給你一個想法..

我希望地圖是一個SVG文件,或者如果它的jpeg可以使用Adobe Illustrator輕松轉換為SVG。 或者還有很多其他選擇。

在這里你可以找到如何將svg轉換為路徑..

在這里,您可以找到如何將EPS(插畫路徑)轉換為CGPath

然后你可以檢查觸摸點是否在路徑內

if([CGPathContainsPoint:myPoint]){

}
else{


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM