【问题标题】:Core Graphics, odd behaviour with CGContextSetShadowWithColor blur height核心图形,CGContextSetShadowWithColor 模糊高度的奇怪行为
【发布时间】:2017-01-11 21:47:03
【问题描述】:

我一直在使用 CGContextSetShadowWithColor 为我的形状添加阴影。我正在尝试使用相同的模糊偏移高度。

但是,我在不同形状上看到的阴影高度是不同的。我不知道为什么会这样。

这是一些典型的代码。

CGSize offset = CGSizeMake(0.1, self.l_shadHeight);
CGContextSetShadowWithColor(context, offset, mauveBlurRadius, mauve.CGColor);

编辑 - 更多代码

+ (NSInteger)setShadowHeight
{
    NSInteger retVal = 10;

    if (IS_IPHONE_6P)
    {
        retVal = 7;
    }
    else if (IS_IPHONE_6)
    {
        retVal = 7;
    }
    else if (IS_IPHONE_5 || TARGET_INTERFACE_BUILDER)
    {
        retVal = 6;

    }
    else if (IS_IPHONE_4_AND_OLDER || IS_IPHONE)
    {
        retVal = 6;
    }
    else if (isiPadPro)
    {
        retVal = 16;
    }
    else if (IS_IPAD)
    {
        retVal = 11;
    }

    return retVal;
}

//

-(void)setSizingClassValues
{
    self.l_shadHeight = [DrawingConstants setShadowHeight];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

    [self setSizingClassValues];

    //// General Declarations
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = UIGraphicsGetCurrentContext();

    //// Shadow Declarations
    //UIColor* circleShadow = shadowColor2;
    UIColor* circleShadow = [shadowColor2 colorWithAlphaComponent: [DrawingConstants SHADOW_ALPHA]];

    CGSize circleShadowOffset = CGSizeMake(0.1, self.l_shadHeight);
    //CGFloat circleShadowBlurRadius = 5;
    CGFloat circleShadowBlurRadius = [DrawingConstants setShadowBlurRadius];

//

    //// Frames
    CGRect frame = self.bounds;

    CGFloat xPadding = 18;
    CGFloat YPadding = 19;
    CGFloat wPadding = 39;
    CGFloat hPadding = 54;

    if (isiPadPro || IS_IPAD)
    {
        if (self.size == sizeSmallerPerformance)
        {
            //so we don't have to rework autolayout on other screens!
            CGFloat fac = 2;
            xPadding = xPadding / fac;
            YPadding = YPadding / fac;
            wPadding = wPadding / fac;
            hPadding = hPadding / fac;
        }
    }

    //// Subframes
    CGRect group = CGRectMake(CGRectGetMinX(frame) + xPadding,
                              CGRectGetMinY(frame) + YPadding,
                              CGRectGetWidth(frame) - wPadding,
                              CGRectGetHeight(frame) - hPadding);

//

    //// Abstracted Attributes
    CGFloat circleSurroundStrokeWidth = self.l_borderWidth;
    CGFloat tongueLeftStrokeWidth = self.l_strokeWidth;;
    CGFloat tongueStrokeWidth = self.l_strokeWidth;

    //// Group
    {
        //// CircleSurround Drawing
        UIBezierPath* circleSurroundPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(group) + floor(CGRectGetWidth(group) * 0.00164) + 0.5, CGRectGetMinY(group) + floor(CGRectGetHeight(group) * 0.00161) + 0.5, floor(CGRectGetWidth(group) * 0.99836) - floor(CGRectGetWidth(group) * 0.00164), floor(CGRectGetHeight(group) * 0.99839) - floor(CGRectGetHeight(group) * 0.00161))];

        CGContextSaveGState(context);
        CGContextSetShadowWithColor(context, circleShadowOffset, circleShadowBlurRadius, circleShadow.CGColor);
        [circleFillColour setFill];
        [circleSurroundPath fill];
        CGContextRestoreGState(context);

        [whiteColour setStroke];
        circleSurroundPath.lineWidth = circleSurroundStrokeWidth;
        [circleSurroundPath stroke];

    }

    //// Cleanup
    CGGradientRelease(innerFaceGradient);
    CGColorSpaceRelease(colorSpace);   
}
@end

【问题讨论】:

  • 您有独立的、可重现的案例吗?两行代码不足以确定任何事情。你确定self.l_shadHeight 总是相同的值吗?你确定你看到的结果是由这个特定的代码引起的,而不是别的吗?
  • 是的 self.l_shadHeight 总是一样的。那条线没有区别。我只是想知道设置为 self.bounds 的帧大小和调整视图大小的自动布局是否会减小大小?感谢您的评论。
  • @KurtRevis 我想我可以添加一些代码来显示它是否有帮助?
  • 这取决于这两行代码绘制的内容。如果这只是在视图的-drawRect: 中,那么我想不出有什么非常常见的方法会导致绘图被重新缩放,尽管有一些方法(例如视图的contentMode 设置得很奇怪)。如果它正在绘制到您稍后使用的图像中,那么它将取决于您如何使用该图像。我可以猜一整天,但运行一个可重现的案例会快得多。
  • 我没有看到明显的错误。我能建议的最好方法是构建一个MCVE,有人可以构建并运行它以查看问题的发生。

标签: ios iphone core-graphics


【解决方案1】:

我发现了这个问题,我在错误的事情上留下了阴影。相反,我将它添加到我最外层的组中。

谢谢@kurtrevis

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多