gpt4 book ai didi

ios - 使用淡入淡出效果动画 CALayer mask 变化

转载 作者:可可西里 更新时间:2023-11-01 17:16:43 32 4
gpt4 key购买 nike

我有一个 UIViewview.layer.mask 设置为 CAShapeLayer 的一个实例。形状层包含一条路径,现在我想通过添加第二个具有偶数/奇数规则的形状来向该形状添加一个洞,并淡入该洞的外观。

问题是添加到路径似乎不是可动画的:

[UIView animateWithDuration:2 animations:^() {
CGPathAddPath(parentPath, nil, holePath);
[v.layer.mask didChangeValueForKey:@"path"];
}];

我该如何制作动画?

最佳答案

经过一些摆弄,找到了解决方法:

  1. 创建一个包含两个子层的层,子层具有两个所需的形状,并将其用作 mask
  2. 将第一个子层(没有孔)的不透明度设置为从 1 到 0。

这是有效的,因为子 CAShapeLayer 实例似乎被用作联合。当你隐藏第一个没有洞的子层时,只会露出洞,共享区域不会改变。

CGMutablePathRef p = CGPathCreateMutable();

// First path
CGPathAddPath(p, nil, outerPath);
CAShapeLayer *mask1 = [CAShapeLayer layer];
mask1.path = p;

// 2nd path with a hole
CGPathAddPath(p, nil, innerPath);
CAShapeLayer *mask2 = [CAShapeLayer layer];
mask2.path = p;
mask2.fillRule = kCAFillRuleEvenOdd;

CGPathRelease(p);

// Create actual mask that hosts two submasks
CALayer *mask = [CALayer layer];
[mask addSublayer:mask1];
[mask addSublayer:mask2];
myView.layer.mask = mask;
mask.frame = v.layer.bounds;
mask1.frame = mask.bounds;
mask2.frame = mask.bounds;

// ...

// Hide mask #1
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"opacity"];
a.fromValue = @1.0;
a.toValue = @0.0;
a.duration = 1.0;
a.fillMode = kCAFillModeForwards; // Don't reset back to original state
a.removedOnCompletion = NO;
[mask1 addAnimation:a forKey:@"hideMask1"];

关于ios - 使用淡入淡出效果动画 CALayer mask 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27638033/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com