【问题标题】:I'm having trouble getting this CA animation to work我无法让这个 CA 动画正常工作
【发布时间】:2017-05-01 22:43:47
【问题描述】:

我正在尝试制作一个动画,其中红色在屏幕上出现脉冲,当点击屏幕时,脉冲的速度会增加。现在我有两个问题。一是动画不发生,二是动画一定要加速,不重启动画。我希望动画从点击屏幕时的任何点开始加速。

class ViewController: UIViewController {

var tapCount: Int = 0
var pulseSpeed: Double = 3
let pulseAnimLayer = CALayer()
let pulseAnim = CABasicAnimation(keyPath: "Opacity")

override func viewDidLoad() {
    super.viewDidLoad()

    counter.center = CGPoint(x: 185, y: 118)

    pulseAnim.fromValue = 0.5
    pulseAnim.toValue = 1.0
    pulseAnim.duration = 3.0
    pulseAnim.autoreverses = true
    pulseAnim.repeatCount = .greatestFiniteMagnitude
    pulseAnimLayer.add(pulseAnim, forKey: "Opacity")
}

func pulseAnimation(pulseSpeed: Double) {
    UIView.animate(withDuration: pulseSpeed, delay: 0,
        options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse],
        animations: {
            self.red.alpha = 0.5
            self.red.alpha = 1.0
        }
    )
}

@IBOutlet weak var red: UIImageView!
@IBOutlet weak var counter: UILabel!

@IBAction func screenTapButton(_ sender: UIButton) {
    tapCount += 1
    counter.text = "\(tapCount)"
    pulseSpeed = Double(3) / Double(tapCount)
    pulseAnim.duration = pulseSpeed
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
}  

【问题讨论】:

    标签: ios swift core-animation uiviewanimation


    【解决方案1】:

    我第一次读的不够仔细。

    你有一个奇怪的混合核心动画和 UIView 动画。两者存在于不同的层面。 Core Animation 将允许您为视图上的属性设置动画(比如它的背景颜色)。

    UIView 动画构建在 Core Animation 之上,通过构建底层 Core Animation 可以更轻松地自动为视图更改设置动画。

    当您创建 CABasicAnimation 时,您从 Core Animation 开始,但随后您有一个函数 pulseAnimation,它看起来正在使用 UIVIew 动画执行某些操作。我认为您可以完全摆脱该功能。

    当您添加 CABasicAnimation 时,您会告诉它更改图层“不透明度”的值。您可能想要的是“不透明度”(小写)。

    您的动画正在应用于您创建的新 CALayer...但您似乎从未将该层放入视图中(因此它从未被绘制)。您很可能应该做的是将您的动画附加到视图的图层,并允许该图层对其颜色进行动画处理。

    要将一个图层添加到另一个图层,请使用 addSublayer (https://developer.apple.com/reference/quartzcore/calayer/1410833-addsublayer)。您代码中的视图层应该可以作为 self.view.layer 使用,尽管您可能必须在 viewWillAppear 而不是 viewDidLoad 中使用它

    【讨论】:

    • 我知道我需要这样做。 UIView 动画和 pulseAnimation 的所有额外内容都应该被删除。我没有在我的最新版本中使用这些。我只是不知道如何将 CALayer 放入视图以及如何在该层上放置动画。
    • 在答案末尾添加了关于如何将子层添加到视图层的评论。
    【解决方案2】:

    你需要在动画之前将红色imageview的alpha设置为0.0

     func pulseAnimation(pulseSpeed: Double) {
    
    
        UIView.animate(withDuration: pulseSpeed, delay: 1.0,
                       options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse],
                       animations: {
                        self.red.alpha = 0.5
                        self.red.alpha = 1.0
        }
        )
    }
    
    override func viewDidAppear(_ animated: Bool) {
    
        self.red.alpha = 0.0
    
    
        counter.center = CGPoint(x: 185, y: 118)
    
    
    }
    

    【讨论】:

    • 为什么alphaUIView animation 中设置了两次?第一个没有效果。
    • 那是真的,但是那部分代码来自问这个问题的人,我没有改变它
    猜你喜欢
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多