我有一个自定义按钮类型的 UIButton,我只是在按钮的底层 CALayer 上设置了背景和边框颜色。然后,我将蒙版应用到这个由 CAShapeLayer 组成的底层(以获得两个圆角),如 this post 中所述。 .
到目前为止一切顺利。我为 UIButton 设置动画以使其变大。到目前为止,一切都很好。见下文(图像中的所有内容都是 _fusedBar,保存您在其下方看到的 x 轴线片段和美元金额文本)。
当我使用相同的动画例程但不同的参数值(仿射平移和同步帧高度更改)为 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
}];
在调用此动画之前,我根据上面引用的帖子设置了 mask 。该代码如下。请注意,“_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;
}
当 _fusedBar 从一个条子增长到几百个点时,增长是动画的,您可以看到它。
在使用这个 CAShapeLayer 掩码之前,当将 _fusedBar 缩小回一条条时,我可以看到动画(上面的代码相同,只是 'scaledHeight' 的值不同)。现在,我看不到收缩动画。
因此,我知道我需要以某种方式摆脱 mask 层的效果,然后再设置收缩操作的动画,但显而易见的事情(对我而言)没有奏效。这是我尝试过的各种事情的示例:
// 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;
很明显,我遗漏了一些 CALayers、掩码甚至可能是 presentationLayer 语义。
非常感谢任何指导!
最佳答案
几天后回过头来,我发现了我的错误(从上面发布的摘录中看不到)。
本质上,在放大或缩小“fusedBar”的动画之后,我根据 View 的框架重新应用了一个 mask 。由于这将有效地与动画 block 一起执行,因此它将有一个 View 缩小到其中的 mask 。
如果 fusedBar 正在收缩,我需要将重新应用掩码代码放入我的动画完成 block 或抑制它。
感谢所有帮助过我的人!
关于iphone - 删除 UIButton 层上的 CAShapeLayer 掩码,以便动画 UIButton 大小更改被取消掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7640277/