仅在另一个 UIImage 内部移动 UIImage [英] Move UIImage only inside of another UIImage

查看:26
本文介绍了仅在另一个 UIImage 内部移动 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIImage,它显示在 UIImageView 中.我在 UIImageView 中还有另一个图像,它位于第一张图像上方.我希望能够仅在第一张图像的边界内拖动第二张图像.为了让我的目标更清晰,请看这张图片:.

I have an UIImage which is shown in an UIImageView. I also have another image in an UIImageView which lays 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 a bit more clearer look at this image: .

绿色大头针应该是可拖动的,但不能将大头针拖入蓝色区域(地图外).目前 pin 是可拖动的,但我不知道如何检查 pin 是否在地图之外.

The green pin should be dragable but it should not be possible to drag the pin into the blue (outside of the map). At the moment the pin is dragable, but I don't know how to check if the pin is outside of the map.

我在我的 UIImageView 子类中使用了这个方法作为可拖动的引脚:

I used this method in my UIImageView subclass for the drag able pin:

- (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 是地图的 UIIImageView 所在的视图控制器.所以我让这个类成为一个单例来获取地图图像.但同样,我为颜色获得的值是完全连线的.我也更新了照片,因为我的用户界面略有不同.

The MapViewController is the Viewcontroller where the UIIImageView for the map is. So i made this class a singleton to get the map-image. But again the values i get for the color are totally wired. Also i updated the photo because my ui got slighty different.

推荐答案

您是否尝试过在自定义 UIViewController 中实现 UIResponder 的触摸方法,然后按如下方式引用 2 个 UIView?

Have you tried implementing the UIResponder's touch methods in your custom UIViewController and then referencing the 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;
    }
}

这篇关于仅在另一个 UIImage 内部移动 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆