移除 UIButton 层上的 CAShapeLayer 遮罩,以便取消遮罩动画 UIButton 大小更改

idS*_*tar 5 iphone core-animation mask objective-c ios

我有一个自定义按钮类型的 UIButton,我只是在按钮的底层 CALayer 上设置了背景和边框颜色。然后我施加掩模,一个CAShapeLayer(得到两个圆角)上述组成的这个下层此篇

到现在为止还挺好。我为 UIButton 设置动画以增大大小。到现在为止还挺好。见下文(图像中的所有内容都是 _fusedBar,保存您在其下方看到的 x 轴线片段和美元金额文本)。

完全成长的 UIButton 在代码中称为 _fusedBar

当我使用相同的动画例程但不同的参数值(同时帧高度变化的仿射平移)为 UIButton 设置动画以使其收缩时,在收缩发生时没有任何 UIButton(除了一条细长条)是可见的。

我试图让作为掩码的 CAShapeLayer 从它的超级层中删除自己,我试图将 UIButton 层上的掩码属性设置为 nil 等,但这些都没有删除掩码,以便我可以看到UIButton 动画(随着仿射平移和框架高度变化而缩小)。

这是相关的动画代码:

// Stackoverflow readers: Assume 'duration', 'scaledHeight' and 'delay' 
// already defined.

void (^fusedBarChange)(void) = ^{    
    _fusedBar.transform = CGAffineTransformMakeTranslation(0, -scaledHeight);
    [_fusedBar nd_setNewSizeHeight:scaledHeight];
};

[UIView animateWithDuration:duration
                      delay:delay
                    options:UIViewAnimationCurveEaseOut 
                 animations:^(void) {
                     fusedBarChange();
                 }
                 completion:^(BOOL finished) {
                     // Other stuff not relevant to _fusedBar animation
                 }];
Run Code Online (Sandbox Code Playgroud)

在调用此动画之前,我根据上面引用的帖子设置了遮罩。该代码如下。请注意,'_fusedBar' 是本次讨论的核心 UIButton。

- (void)configureRoundedTop {
    // YES: Start by creating the path (with only the top-left corner rounded)
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_fusedBar.bounds 
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                         cornerRadii:CGSizeMake(4.0, 4.0)];

    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = _fusedBar.bounds;
    maskLayer.path = maskPath.CGPath;

    // Set the newly created shape layer as the mask for the image view's layer
    _fusedBar.layer.mask = maskLayer;
}
Run Code Online (Sandbox Code Playgroud)

当 _fusedBar 从一个条子增长到几百点时,增长是动画的,你可以看到它。

在使用这个 CAShapeLayer 蒙版之前,当将 _fusedBar 缩小回条状时,我可以看到动画(上面的代码相同,只是'scaledHeight'的值不同)。现在,我看不到缩小的动画。

所以,我知道我需要在动画收缩操作之前以某种方式摆脱遮罩层的影响,但明显的事情(对我来说)没有奏效。这是我尝试过的各种事情的一个例子:

// None of these do the trick. I call them just before 
// the shrinking animation is invoked:
[_fusedBar setNeedsLayout];
[_fusedBar setNeedsDisplay];
[_fusedBar.layer.mask removeFromSuperlayer];
_fusedBar.layer.mask = nil;
Run Code Online (Sandbox Code Playgroud)

显然,我错过了一些 CALayer、掩码甚至presentationLayer 语义。

非常感谢任何指导!

idS*_*tar 0

几天后回过头来,我发现了我的错误(从上面发布的摘录中看不到)。

本质上,在动画放大或缩小“fusedBar”之后,我根据视图的框架重新应用蒙版。由于这将有效地与动画块一起执行,因此它将有一个视图缩小到的遮罩。

我需要将重新应用掩码代码放入动画完成块中,或者在 fusedBar 缩小时抑制它。

感谢所有看过并提供帮助的人!