2016-07-20 45 views
0

我要創建使用核心動畫像動畫:iOS版的Core Animation:顯示和隱藏效果

  • 圖片A從1秒到3秒顯示。 3秒後,隱藏它。
  • 圖片B顯示從5秒到7秒。 7s後,隱藏它。

我的代碼:

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") 

我如何能實現呢?謝謝:)

+0

可以顯示烏爾試圖代碼 –

+0

在同一圖像視圖?如果沒有任何東西可以顯示,從0到1s以及3s到5s之間,你會希望圖像爲空 –

+0

@nbubu:我添加了我的代碼,請刷新看看:) –

回答

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() 
    }) 

}