【问题标题】:CAKeyframeAnimation rotation don't draw real shadowCAKeyframeAnimation 旋转不绘制真实阴影
【发布时间】:2017-01-13 11:35:45
【问题描述】:

我想用 CAKeyframeAnimation 旋转 UIImageView。问题是阴影随着物体旋转,做出了非真实的效果...The shadow must remain under the image, not rotating around it

这是我的代码。很简单:

    let imagen:UIImageView = UIImageView(image:UIImage(named: "material6.png"))

    self.view.addSubview(imagen)

    imagen.center = CGPoint(x: 100, y: 100)

    imagen.layer.shadowOpacity = 0.75
    imagen.layer.shadowOffset = CGSize(width: 0, height: 15)

    //Animación del ángulo
    let animacionAngulo = CAKeyframeAnimation(keyPath: "transform.rotation.z")

    var valoresAngulo = [NSNumber]()
    let degrees:Float = 360
    let radians = degrees * Float.pi / Float(180.0)

    valoresAngulo.append(NSNumber(value: 0))
    valoresAngulo.append(NSNumber(value: radians))

    animacionAngulo.values = valoresAngulo

    //Permite añadir varias animaciones y gestionarlas a la vez
    animacionAngulo.repeatCount = Float.infinity
    animacionAngulo.duration = 2.0

    imagen.layer.add(animacionAngulo, forKey: nil)

有什么办法吗?

【问题讨论】:

  • 请查看我的回答,如果达到你想要的效果,请告诉我。
  • 是的,它很完美......我必须稍微更改代码,因为我的问题只是整个动画的一部分(位置、shadowOffset、大小......),但我的问题是关于阴影方向。所以,解决了!再次感谢

标签: ios objective-c swift shadow cakeyframeanimation


【解决方案1】:

如果阴影与图像一起旋转。您可以通过在UIImageView 下方添加与UIImageView 相同尺寸的UIView 来解决此问题。如果你给你的UIView添加阴影而不是UIImageView,你可以在这种情况下达到预期的效果。

let imagen:UIImageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
imagen.center = CGPoint(x: 100, y: 100)
imagen.backgroundColor = UIColor.red

let belowView: UIView = UIView(frame: imagen.frame)
belowView.backgroundColor = UIColor.white
belowView.layer.shadowOpacity = 0.75
belowView.layer.shadowOffset = CGSize(width: 0, height: 15)
self.view.addSubview(belowView)

self.view.addSubview(imagen)

    //Animación del ángulo
let animacionAngulo = CAKeyframeAnimation(keyPath: "transform.rotation.z")

var valoresAngulo = [NSNumber]()
let degrees:Float = 360
let radians = degrees * Float.pi / Float(180.0)

valoresAngulo.append(NSNumber(value: 0))
valoresAngulo.append(NSNumber(value: radians))

animacionAngulo.values = valoresAngulo

    //Permite añadir varias animaciones y gestionarlas a la vez
animacionAngulo.repeatCount = Float.infinity
animacionAngulo.duration = 2.0

imagen.layer.add(animacionAngulo, forKey: nil)

【讨论】:

  • 非常感谢!!!我已经为此奋斗了好几天...正在寻找解决方案,但我还没有找到...您拯救了我的一周! ;)
猜你喜欢
  • 2011-08-12
  • 2021-02-09
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 2012-02-16
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多