iphone - CGContextSetShadow() - iOS 3.0 和 4.0 之间的阴影方向颠倒了?

标签 iphone quartz-graphics

我一直在 iPhone 上的 Quartz 绘图代码中使用 CGContextSetShadowWithColor() 来生成文本和其他内容的“踩踏”外观(在 drawRect: 中)和drawLayer:inContext:)。

工作完美,但是当针对 iOS 3.2 和现在的 iOS 4.0 运行完全相同的代码时,我注意到阴影都在相反的方向。例如。在下面的代码中,我将黑色阴影设置为文本上方 1 像素,这使其具有“压入”外观,现在该阴影位于文本下方 1 像素,给它一个标准的阴影。

...
CGContextSetShadowWithColor(context, CGSizeMake(0.f, 1.f), 0.5f, shadowColor);
CGContextShowGlyphsAtPoint(context, origin.x, origin.y, glyphs, length);
...

现在我不知道我是否(或曾经)做错了什么,或者此设置的处理是否发生了变化。我没有应用任何可以向我解释这一点的转换,至少没有意识到。我在一个实例中翻转了文本矩阵,但在其他实例中没有翻转,并且这种行为是一致的。另外,我在 SDK 发行说明中找不到任何有关此内容的信息,所以看来可能是我干的。可能是什么问题?

最佳答案

所以这似乎要么是一个错误,要么是苹果故意的;无论哪种方式,为了解决这个问题,我现在使用 UIView 类别。我设置了 iOS 3.2+ 上应有的阴影 Y 方向,但只要我支持 iOS < 3.2,我将使用以下类方法将方向乘以 1 或 -1,根据需要设备:

...
CGContextSetShadowWithColor(context, CGSizeMake(0.f, [UIView shadowVerticalMultiplier] * -1.f), 0.5f, textShadowColor);
...

类别如下:

@interface UIView (shadowBug) 

+ (NSInteger) shadowVerticalMultiplier;    

@end


@implementation UIView (shadowBug)

+ (NSInteger) shadowVerticalMultiplier
{
    static NSInteger shadowVerticalMultiplier = 0;
    if (0 == shadowVerticalMultiplier) {
        CGFloat systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
        shadowVerticalMultiplier = (systemVersion < 3.2f) ? -1 : 1;
    }

    return shadowVerticalMultiplier;
}


@end

关于iphone - CGContextSetShadow() - iOS 3.0 和 4.0 之间的阴影方向颠倒了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2997501/

相关文章:

ios - navigator.notification.alert 不适用于 ios 的 cordova

ios - 在 iOS 11.0.3 中插入​​注释 View 失败

macos - Cocoa 中的同级 NSView z 排序

iphone - 具有选项卡之间分页功能的选项卡栏应用程序

iphone - 在单点触控中显示来自 Byte[] 的图像

iphone - quartz 2D : draw from a CGContext to another CGContext

iphone - 在 iPhone 上使 UIImageView 不透明时出现问题

iphone - 尝试使用 CAGradientLayer 时出现编译错误

iphone - 在仿射变换上放大 iPhone "vector-based graphics"

iphone - 如何让 NSTimer 在 iOS 7 的后台保持事件状态?