iOS核心动画:显示和隐藏效果

时间:2016-07-20 09:47:26

标签: ios animation

我想使用Core Animation创建动画,如:

  
      
  • 图像A显示为1s至3s。 3秒后,隐藏它。
  •   
  • 图像B显示为5s至7s。 7秒后,隐藏它。
  •   

我的代码:

    var animation = CABasicAnimation(keyPath: "opacity")
    animation.duration = 3.0
    // animate from fully visible to invisible
    animation.fromValue = NSNumber(float: 1.0)
    animation.toValue = NSNumber(float: 0.0)
    animation.beginTime = 2
    animation.removedOnCompletion = true
    layer1.addAnimation(animation, forKey: "animateOpacity")

    animation = CABasicAnimation(keyPath: "opacity")
    animation.duration = 3.0
    // animate from fully visible to invisible
    animation.fromValue = NSNumber(float: 1.0)
    animation.toValue = NSNumber(float: 0.0)
    animation.beginTime = 5
    animation.removedOnCompletion = true
    layer2.addAnimation(animation, forKey: "animateOpacity")

我该如何实施?谢谢:))

1 个答案:

答案 0 :(得分:1)

它可以以各种方式进行

检查链接... 的 http://www.appcoda.com/view-animation-in-swift/

override func viewWillAppear(animated: Bool) {

    let firstImageView = UIImageView(image: UIImage(named: "bg01.png"))
    firstImageView.frame = view.frame
    view.addSubview(firstImageView)

    imageFadeIn(firstImageView)

}

func imageFadeIn(imageView: UIImageView) {

    let secondImageView = UIImageView(image: UIImage(named: "bg02.png"))
    secondImageView.frame = view.frame
    secondImageView.alpha = 0.0

    view.insertSubview(secondImageView, aboveSubview: imageView)

    UIView.animateWithDuration(2.0, delay: 2.0, options: .CurveEaseOut, animations: {
        secondImageView.alpha = 1.0
        }, completion: {_ in
            imageView.image = secondImageView.image
            secondImageView.removeFromSuperview()
    })

}