ios - 为 CALayer 的 shadowPath 设置动画时的平滑动画

标签 ios objective-c animation uikit core-animation

在我的应用程序中,我有一个使用 CALayer 来实现阴影的 UIView:

@implementation MyUIView

    - (instancetype) initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if(!self) return self;

        self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:.2].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 1;

        return self;
    }

@end

如果我想要任何接近合理性能的东西,我必须定义 CALayershadowPath:

@implementation MyUIView

    - (void) setFrame:(CGRect)frame {
        [super setFrame:frame];

        self.layer.shadowPath = CGPathCreateWithRect(self.bounds, NULL);
    }

@end

每当我为这个 UIView 设置动画时,我注意到两件事:

  1. 如果我使用shadowPath,阴影会随着旋转和帧大小的变化而很好地动画化。这里需要注意的是动画速度非常慢,并且普遍缺乏性能。

  2. 如果我使用shadowPath 每当UIView 动画时动画是流畅和及时的,但是阴影的过渡本身与没有 shadowPath 的情况相比,它更像 block (并且更不平滑)。

示例:

编辑:

值得注意的是,这些动画是隐式的——我自己并没有调用它们。它们是 UIViewController 随设备方向旋转的结果。阴影位于 UIView 上,在旋转期间会改变大小。

最佳答案

我试图重现您提供的两个 gif 中显示的行为,但没有成功(也许您可以使用动画代码编辑您的问题,例如 UIView animateWithDuration:animations:)。

然而,在我脑海深处的某个地方,我记得我偶尔遇到过类似的问题,结果我不得不光栅化 View 以使其平滑。

所以我不能保证它能解决你的问题,但试一试:

self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [[UIScreen mainScreen] scale];

关于ios - 为 CALayer 的 shadowPath 设置动画时的平滑动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28787395/

相关文章:

ios - ShareKit - Flickr 集成 - 想要绕过(或至少自动填充)属性对话框

ios - 将 UIImage 从 View Controller 传递到另一个

iphone - 如何在iPhone SDK中在后台运行闹钟?

objective-c - CGPDFPageRef 到 CGImageRef/NSBitmapImageRep 无需绘制

swift - 线性 CABasicAnimation 在容器 View 中不是线性执行的

objective-c - 核心音频 : is zero equivalent to silence only for PCM audio?

objective-c - 为 UINavigationController 设置默认的 rightBarButtonItem

ios - "Stack"UIViewController 中的 UIViews (iOS 7)

css - css 动画中的图像在 firefox 中显示之前未完全加载

javascript - CSS3 关键帧动画 : End and stay on the last frame