以 CAShapeLayer 作为遮罩的 UIView 子类无法正确设置动画

发布于 2025-01-07 00:34:40 字数 771 浏览 5 评论 0原文

我通过添加图层蒙版向子类 UIView 添加了圆角。以下是 Montouch 代码,但在 ObjC 中也应该很容易理解其含义:

// Add a layer that holds the rounded corners.
UIBezierPath oMaskPath = UIBezierPath.FromRoundedRect (this.Bounds, this.eRoundedCorners, new SizeF (this.fCornerRadius, this.fCornerRadius));

private void UpdateMask()
{           
if(this.Layer.Mask == null)
{
    CAShapeLayer oMaskLayer = new CAShapeLayer ();
    oMaskLayer.Frame = this.Bounds;

    // Set the newly created shape layer as the mask for the image view's layer
    this.Layer.Mask = oMaskLayer;
}
((CAShapeLayer)this.Layer.Mask).Path = oMaskPath.CGPath;
}

我已经重载了 Frame 属性。设置框架可调整蒙版以保持圆角形状。

但是,如果我对帧大小的变化进行动画处理,则蒙版将立即设置为动画的目标值。

有没有办法让我的视图检测到它是动画的一部分并使蒙版正确动画?

I have added round corners to my subclassed UIView by adding a layer mask. The following is Montouch code, but should be easy enough to get the meaning also in ObjC:

// Add a layer that holds the rounded corners.
UIBezierPath oMaskPath = UIBezierPath.FromRoundedRect (this.Bounds, this.eRoundedCorners, new SizeF (this.fCornerRadius, this.fCornerRadius));

private void UpdateMask()
{           
if(this.Layer.Mask == null)
{
    CAShapeLayer oMaskLayer = new CAShapeLayer ();
    oMaskLayer.Frame = this.Bounds;

    // Set the newly created shape layer as the mask for the image view's layer
    this.Layer.Mask = oMaskLayer;
}
((CAShapeLayer)this.Layer.Mask).Path = oMaskPath.CGPath;
}

I have overloaded the Frame property. Setting the frame adjusts the mask to keep the rounded corners in shape.

However if I animate the change in frame size, the mask is immediately set to the destination value of the animation instead.

Is there a way to make my view detect that is is part of an animation and make the mask animate correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2025-01-14 00:34:40

这就是 CoreAnimation 显式动画的工作原理。

使用显式动画,您实际上永远不会更改原始对象的值,您只需为表示层中的对象设置动画即可。因此,在开始动画之前,您需要确保对象已设置为您最终想要的值。

WWDC 2011 在线视频的“Core Animation Essentials”中对如何解决此类问题进行了完整说明。查找会话 421

This is how CoreAnimation works for explicit animations.

With explicit animations, you never actually alter the values of your original objects, you merely animate objects in the presentation layer. So you need to make sure that your object has been setup before you start the animation with the value that you want to have in the end.

A complete explanation of how to solve this class of problems is explained in "Core Animation Essentials" on the WWDC 2011 online videos. Look for session 421

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文