如何使用CATransform3D向一组CALayer添加阴影

时间:2012-08-15 09:01:07

标签: objective-c ios calayer uibezierpath cgpath

如何为一组CALayers添加阴影? 我有一个“FoldingView”类,它包含几个“SliceView”类。每个sliceview的图层都将被赋予一个CATransform3D,我正在使用透视属性(.m34 = 1.0 / -1000)。

如何添加具有良好视觉逻辑的阴影?这是我到目前为止所想的:

  1. 我可以获取每个切片的路径并将它们组合起来以获得阴影路径。

    • 我不知道当图层使用CATransform3D时如何获取CALayer的路径
    • 它可能在视觉上有效,但我担心如果光线应该来自左上角那么它就不会完全正确。
  2. 我可以将标准CALayer阴影应用于所有图层

    • 由于阴影相互重叠,它看起来不太好
  3. 如果有人有任何其他建议或知道如何编写1号创意,我将非常高兴!以下是您从屏幕截图中看到的示例项目的链接。

    Download zipped application with code

    Sample image from application

1 个答案:

答案 0 :(得分:1)

这似乎按照需要工作。这是根据sliceview完成的。

- (UIBezierPath *)shadowPath
{ 

    if(self.progress == 0 && self.position != VGFoldSliceCenter)
    {
        UIBezierPath *path = [UIBezierPath bezierPath];  
        return path;
    }
    else
    { 
        CGPoint topLeft = pointForAnchorPointInRect(CGPointMake(0, 0), self.bounds);
        CGPoint topRight = pointForAnchorPointInRect(CGPointMake(1, 0), self.bounds);
        CGPoint bottomLeft = pointForAnchorPointInRect(CGPointMake(0, 1), self.bounds);
        CGPoint bottomRight = pointForAnchorPointInRect(CGPointMake(1, 1), self.bounds);

        CGPoint topLeftTranslated = [self.superview convertPoint:topLeft fromView:self];
        CGPoint topRightTranslated = [self.superview convertPoint:topRight fromView:self];
        CGPoint bottomLeftTranslated = [self.superview convertPoint:bottomLeft fromView:self];
        CGPoint bottomRightTranslated = [self.superview convertPoint:bottomRight fromView:self];

        UIBezierPath *path = [UIBezierPath bezierPath];

        [path moveToPoint:topLeftTranslated];
        [path addLineToPoint:topRightTranslated];
        [path addLineToPoint:bottomRightTranslated];
        [path addLineToPoint:bottomLeftTranslated];
        [path closePath]; 

        return path;
    } 
}