ios - CAKeyframeAnimation 旋转不绘制真实阴影

标签 ios objective-c swift shadow cakeyframeanimation

我想用 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)

有什么解决办法吗?

最佳答案

如果影子随着图像旋转。您可以通过在 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)

关于ios - CAKeyframeAnimation 旋转不绘制真实阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41633770/

相关文章:

javascript - 有没有一种方法可以将文件保存到设备(android 或 ios)内存,而无需在 Web 应用程序中使用phonegap File Api

ios - 如果我将使用Swift 3再次运行该应用程序,则该图像在文档目录中不存在

ios - 在 swift 3 的主细节 View Controller 的细节 View Controller 中呈现 View Controller

ios - 对于在应用程序的生命周期中应该存在的单例类来说,强保留周期是否是一个相关的考虑因素?

c++ - 将 C++ 中的运算符 == 转换为 Objective-C

ios - 如何获取 base64 字符串的 icloud PDF 文件路径? swift 3

ios - 使用 Alamofire '~> 5.0.0-beta.3' 从 multipart-form-data 上传图像

objective-c - 防止在公共(public)接口(interface)中将对象添加到 NSMutableArray

ios - 如何在 swift 中定义 HEX 颜色宏

json - 解码 JSON Swift 4 - 嵌套对象/数组