为什么我无法为UIView的影子制作动画?

时间:2016-02-22 19:34:44

标签: ios cocoa-touch uiview core-animation calayer

我想在我的UIView上制作阴影动画,以便它开始可见,并且在动画结束时是不可见的。我可以做相反的事情,但是如果我尝试淡出它,动画永远不会发生,事实上我从来没有看到过舔阴影。

我的代码:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let foo = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
    foo.backgroundColor = UIColor.greenColor()
    applyShadowToView(foo)
    view.addSubview(foo)

    foo.layer.shadowOpacity = 1.0

    let shadowAnimation = CABasicAnimation(keyPath: "shadowOpacity")
    shadowAnimation.fromValue = 1.0
    shadowAnimation.toValue = 0.0
    shadowAnimation.duration = 0.5
    shadowAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    foo.layer.addAnimation(shadowAnimation, forKey: "shadowOpacity")
    foo.layer.shadowOpacity = 0.0
}

private func applyShadowToView(view: UIView) {
    view.clipsToBounds = false
    view.layer.shadowColor = UIColor(white: 0.4, alpha: 1.0).CGColor
    view.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
    view.layer.shadowRadius = 5.0
    view.layer.shadowOpacity = 0.0
    view.layer.shadowPath = UIBezierPath(rect: view.bounds).CGPath
    view.layer.shouldRasterize = true
    view.layer.rasterizationScale = UIScreen.mainScreen().scale
}

我到底做错了什么?

0 个答案:

没有答案