2

我有它,用户可以在屏幕上移动图像和 uilabel。假设它是黑色图像和黑色文本。我希望与图像重叠的文本部分变得透明,以便背景显示出来,即使它们最初是相同的颜色,您仍然可以阅读文本。希望这是有道理的。

我找到了一些关于触摸擦除的链接。我不确定如何使用重叠的文本部分自动执行此操作。

也许像这样“用图像掩盖图像”? https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html

重叠文本示例

这是我移动图像(以及文本)的方式

-(void)graphicMoved:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];


}

添加的代码 在将文本和图像部分重叠放置后,我在单击按钮时调用 creatAlphaClippedImage。图像是 imageMerge,标签是 labelTest。

我所做的只是在开始时分配它们,然后在最后,获取输出图像并将其分配给 imageMerge.image,该图像被添加到我的视图中,界面生成器在角落里只是为了测试。我不知道 nslog 应该显示什么,但它不是空的。

-(void)createAlphaClippedImage {
UIImageView *imageView = nil;//this will b your imageView, property that u are holding to
UILabel *label = nil;//this will be your label, property that u are holding to


imageView=imageMerge;
label=labelTest;


if (CGRectIntersectsRect(imageView.frame, label.frame)) {
    UIImage *bottomImage = imageView.image;

    UIImage *image = nil;
    UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
    CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
    [bottomImage drawInRect:imageRect];
    //the color that u want the text to have
    [[UIColor clearColor] set];
    //change this textRect to change the position of text on image
    CGRect textRect = CGRectMake(CGRectGetMinX(label.frame) - CGRectGetMinX(imageView.frame), CGRectGetMinY(label.frame) - CGRectGetMinY(imageView.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame));
    [label.text drawInRect:textRect withFont:label.font];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    imageView.image = image;


    imageMerge.image=image;

    NSLog(@"ImageMerge %@\n",imageMerge.image);
}
}
4

1 回答 1

2

假设您已经完成了在屏幕上移动 imageView 的操作。让我继续做什么。

  1. 当你触摸 imageView。使用我将提供的代码更改其中的图像。
  2. 完成图像视图的移动后,将新图像替换为旧图像

新图片=一张黑色图片+黑色文字;旧图=一张黑图+透明文字;

-(UIImage *)customImage:(NSString *)cellImageName withText:(NSString *)badgeText textColor:(UIColor*)color {
    UIImage *bottomImage = [UIImage imageNamed:cellImageName];
    //Change the font size to change text size
    CGSize stringSize = [badgeText sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];

    UIImage *image = nil;
    UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
    CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
    [bottomImage drawInRect:imageRect];
    //the color that u want the text to have
    [color set];
    //change this textRect to change the position of text on image
    CGRect textRect = CGRectMake((bottomImage.size.width - stringSize.width)/2.0f, bottomImage.size.height - stringSize.height, stringSize.width, stringSize.height);
    [badgeText drawInRect:textRect withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

现在在你开始之前,扔掉标签,为了方便起见(如果你愿意,你可以用 CALayer 替换 UIImageView),而不是

imageView.image = image你可以写layer.contents = (id)image.CGImage

现在,当您设置您的 ImageView 或图层时,首先将图像获取为

UIImage *image = [self customImage:@"Image Name" withText:@"Text" textColor:[UIColor blackColor]];

并将此图像放入 imageView 或图层

现在,当您开始移动图像视图或图层时。在touchesBegan或任何一种方法是您的第一响应者触摸。再次将图像替换为

UIImage *image = [self customImage:@"Image Name" withText:@"Text" textColor:[UIColor clearColor]];

touchesEnded用第一次调用黑色文本再次替换图像。

这应该让你继续前进。如有问题请告诉我。

上述方法提供在图像顶部写入文本,并创建一个包含文本的新图像。因此,您可以丢弃标签。

干杯,玩得开心。

编辑

试试这个,我自己没试过,但应该可以

-(void)createAlphaClippedImage {
    UIImageView *imageView = nil;//this will b your imageView, property that u are holding to
    UILabel *label = nil;//this will be your label, property that u are holding to

    if (CGRectIntersectsRect(imageView.frame, label.frame)) {
        UIImage *bottomImage = imageView.image;

        UIImage *image = nil;
        UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
        CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
        [bottomImage drawInRect:imageRect];
        //the color that u want the text to have
        [[UIColor clearColor] set];
        //change this textRect to change the position of text on image
        CGRect textRect = CGRectMake(CGRectGetMinX(label.frame) - CGRectGetMinX(imageView.frame), CGRectGetMinY(label.frame) - CGRectGetMinY(imageView.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame));
        [label.text drawInRect:textRect withFont:label.font];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        imageView.image = image;
    }
}

在 touchesBegan 和 touchesMoved 中调用此方法。如果动画卡顿(这不会发生),请尝试在动画块内调用它。

试试这个,让我知道。

编辑:链接到示例应用程序

我创建了一个示例应用程序,展示了如何清除重叠区域。核实。https://www.dropbox.com/sh/5z45gkwgmstfyvu/lwzbUyh6IR

于 2013-02-11T13:24:06.020 回答