ios - CAShapeLayer mask 从底部显示

标签 ios swift calayer cashapelayer

我正在尝试从图像底部创建“显示”效果。 我认为这就像将 anchorPoint 设置为 CGPointMake(0.5, 1.0) 或将 contentsGravity 设置为 kCAGravityTop 一样简单,但这些选项都不起作用。

我的当前代码有效,但从上到下进行动画处理。这是揭示的想法:http://giphy.com/gifs/xTiTnBzItdgaD1xHMc

我如何使它从下到上进行?

这是代码

    let path                = UIBezierPath(rect: CGRectMake(0, 0, imgEmptyBase.width, imgEmptyBase.height - 80))
    let mask                = CAShapeLayer()
    mask.anchorPoint        = CGPointMake(0.5, 1.0)
    mask.path               = path.CGPath

    imgEmptyBase.layer.mask = mask

    let anim                = CABasicAnimation(keyPath: "path")

    anim.fromValue          = path.CGPath
    anim.toValue            = UIBezierPath(rect: imgEmptyBase.bounds).CGPath
    anim.duration           = 1.0
    anim.fillMode           = kCAFillModeForwards
    anim.removedOnCompletion = false

    imgEmptyBase.layer.mask.addAnimation(anim, forKey: "anim")

最佳答案

这将完全达到您想要的效果:

let path = UIBezierPath(rect: CGRectMake(0, imgEmptyBase.layer.frame.size.height, imgEmptyBase.layer.frame.size.width, imgEmptyBase.layer.frame.size.height))
let mask = CAShapeLayer()
mask.path = path.CGPath

imgEmptyBase.layer.mask = mask

let revealPath = UIBezierPath(rect: CGRectMake(0, 0, imgEmptyBase.layer.frame.size.width, imgEmptyBase.layer.frame.size.height))

let anim                = CABasicAnimation(keyPath: "path")
anim.fromValue          = path.CGPath
anim.toValue            = revealPath.CGPath
anim.duration           = 1.0
anim.fillMode           = kCAFillModeForwards
anim.removedOnCompletion = false

imgEmptyBase.layer.mask.addAnimation(anim, forKey: "anim")

这一切都是关于使用正确的“从”和“到”值更改蒙版路径。例如:将初始 mask 路径设置为 CGRectMake(0, 0, imgEmptyBase.layer.frame.size.width, 0) 并将 revealPath 设置为 CGRectMake( 0, 0, imgEmptyBase.layer.frame.size.width, imgEmptyBase.layer.frame.size.height) 会产生“从上到下”的显示效果。

关于ios - CAShapeLayer mask 从底部显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502528/

相关文章:

ios - 在 UIWebview Swift 中居中嵌入的 youtube 视频

ios - 奇怪的 UICollectionViewCell 行为如果 float 大小

ios - 无法从 Apple APN 生产服务器获得响应

ios - Swift:视觉格式语言:填充标签和修复按钮

swift - 如何通过代码而不是拖放将 Storyboard中的多个按钮连接到一个 Action ?

Swift:带有圆顶边缘和彩色边框的 UIVisualEffectView

objective-c - 在 Objective-C 的 CALayer 中绘制圆弧和矩形

iOS: Realm ,检查对象是否为零

ios - 自定义 UITabBarController 移动到顶部留下白色区域

swift - 无法为自定义 View 设置约束,Swift