利用 UIImageView 實現多張圖片連續播放的動畫

Hi,大家好最近很久都沒交作業了!不是都沒有寫作業,只是因為最近要展覽了所以都是在用公司的App,所以荒於學業~~

這項作業也剛好是我有用在公司的App上面的畫面,不過目前只是示意圖沒有連接硬體設備,所以還只是半殘廢的作品。

以下是程式碼囉!

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

//背景動畫

var imageView = UIImageView(frame: CGRect(x: 0, y: 64, width: 414, height: 623))

var images = [UIImage(named: “roadbackground1”)!, UIImage(named: “roadbackground2”)!, UIImage(named: “roadbackground3”)!]

var animatedImage = UIImage.animatedImage(with: images, duration: 3)

imageView.image = animatedImage

view.addSubview(imageView)

view.sendSubviewToBack(imageView) //讓圖到最底層

//左側BSD警示

imageView = UIImageView(frame: CGRect(x: 0, y: 290, width: 145, height: 145))

images = [UIImage(named: “左側警示”)!, UIImage(named: “左側未警示”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 0.5)

imageView.image = animatedImage

view.addSubview(imageView)

//右側BSD警示

imageView = UIImageView(frame: CGRect(x: 270, y: 290, width: 145, height: 145))

images = [UIImage(named: “右側警示”)!, UIImage(named: “右側未警示”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 2)

imageView.image = animatedImage

view.addSubview(imageView)

//左前TPMS警示

imageView = UIImageView(frame: CGRect(x: 117, y: 410, width: 54, height: 67))

images = [UIImage(named: “胎壓正常”)!, UIImage(named: “胎壓異常”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 2)

imageView.image = animatedImage

view.addSubview(imageView)

//右前TPMS警示

imageView = UIImageView(frame: CGRect(x: 242, y: 410, width: 54, height: 67))

images = [UIImage(named: “胎壓正常”)!, UIImage(named: “胎壓異常”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 2)

imageView.image = animatedImage

view.addSubview(imageView)

//右後TPMS警示

imageView = UIImageView(frame: CGRect(x: 242, y: 564, width: 54, height: 67))

images = [UIImage(named: “胎壓正常”)!, UIImage(named: “胎壓異常”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 2)

imageView.image = animatedImage

view.addSubview(imageView)

//左後TPMS警示

imageView = UIImageView(frame: CGRect(x: 117, y: 564, width: 54, height: 67))

images = [UIImage(named: “胎壓正常”)!, UIImage(named: “胎壓異常”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 2)

imageView.image = animatedImage

view.addSubview(imageView)

//行人偵測警示

imageView = UIImageView(frame: CGRect(x: 132, y: 64, width: 150, height: 109))

images = [UIImage(named: “行人偵測”)!, UIImage(named: “行人偵測-1”)!]

animatedImage = UIImage.animatedImage(with: images, duration: 0.1)

imageView.image = animatedImage

view.addSubview(imageView)

}

}

其中有一個地方需要注意的是!因為車子本身我是用storyboard直接拉一個image view,所以在用程式寫的時候,車子會被蓋到最後面直接消失

好險上次有問彼得潘,他就在我的程式裡加了這一行。 view.sendSubviewToBack(imageView) //讓圖到最底層

多重要的一行啊~~~~~

--

--