【问题标题】:how to darken a UIImageView如何使 UIImageView 变暗
【发布时间】:2010-10-23 23:01:37
【问题描述】:

当 UIImageView 被触摸时,我需要将其变暗,几乎就像跳板上的图标(主屏幕)一样。

我应该添加 0.5 alpha 和黑色背景的 UIView。这似乎很笨拙。我应该使用图层还是其他东西(CALayers)。

【问题讨论】:

  • 可以改用UIButton吗?

标签: iphone objective-c uiimageview layer


【解决方案1】:

我会让 UIImageView 处理图像的实际绘制,但将图像切换到预先变暗的图像。这是我用来生成保持 alpha 的深色图像的一些代码:

+ (UIImage *)darkenImage:(UIImage *)image toLevel:(CGFloat)level
{
    // Create a temporary view to act as a darkening layer
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    UIView *tempView = [[UIView alloc] initWithFrame:frame];
    tempView.backgroundColor = [UIColor blackColor];
    tempView.alpha = level;

    // Draw the image into a new graphics context
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [image drawInRect:frame];

    // Flip the context vertically so we can draw the dark layer via a mask that
    // aligns with the image's alpha pixels (Quartz uses flipped coordinates)
    CGContextTranslateCTM(context, 0, frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextClipToMask(context, frame, image.CGImage);
    [tempView.layer renderInContext:context];

    // Produce a new image from this context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *toReturn = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    UIGraphicsEndImageContext();
    [tempView release];
    return toReturn;
}

【讨论】:

  • 谢谢!这就是我要找的。​​span>
【解决方案2】:

如何继承 UIView 并添加 UIImage ivar(称为图像)?然后你可以覆盖 -drawRect: 类似这样的东西,前提是你有一个名为 press 的布尔 ivar,它是在触摸时设置的。

- (void)drawRect:(CGRect)rect
{
[image drawAtPoint:(CGPointMake(0.0, 0.0))];

// if pressed, fill rect with dark translucent color
if (pressed)
    {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 0.5);
    CGContextFillRect(ctx, rect);
    CGContextRestoreGState(ctx);
    }
}

您可能想尝试使用上述 RGBA 值。当然,非矩形形状需要更多的工作——比如 CGMutablePathRef。

【讨论】:

  • 有问题的 UIImage 恰好在 UIView 的子类中,所以我可以这样做。但是为什么我会在drawRect方法中这样做,我不能直接在touch开始时这样做吗?
  • 如果是切换某个设置的问题,那么这将在 touchesBegan 中发生。它可能会在 touchesEnded 中切换回来。但我认为,实际的绘图会发生在 drawRect 中。您可能需要结合切换状态更改调用 UIView 子类实例上的 setNeedsDisplay。 (这可能最好在自定义设置器中完成。)我不确定如果 UIImageView 的子类的 drawRect 被覆盖,它将如何表现。这就是为什么我建议基本上编写自己的 UIImageView 的“更安全”的方法。希望这会有所帮助。
  • 你误解了我的评论,为什么自定义绘图必须发生在drawRect中。为什么我不能把所有的 CGContext... 代码放在 touchesBegan 中。
  • @Jonathan,引用 Joe Conway 和 Aaron Hillegass 在他们的书“iPhone 编程:The Big Nerd Ranch Guide”的第 91 页中的话,“drawRect: 方法是视图的绘图代码所在。 '我总是在 drawRect: 中绘制核心图形。您可能想通读此 developer.apple.com/library/ios/#documentation/WindowsViews/…> 以了解更多详细信息。我一直无法找到警告不要在drawRect之外绘图的文档:;以为我读到了类似的东西……
【解决方案3】:

UIImageView 可以有多个图像;您可以有两个版本的图像,并在需要时切换到较暗的版本。

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多