CAKey帧动画旋转不绘制真实阴影



我想使用 CAKeyframeAnimation 旋转 UIImageView。问题是阴影随着物体旋转,产生非真实效果......阴影必须保持在图像下方,而不是围绕图像旋转

这是我的代码。很简单:

    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)

有什么解决办法吗?

如果阴影与图像一起旋转。您可以通过在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)

最新更新