コード記述
Storyboardには何も加えなくてOKです、表示させたい画像だけAssets内に入れておいてください。
import UIKit
class ViewController: UIViewController {
private var imageArray:Array<UIImageView> = Array()
override func viewDidLoad() {
super.viewDidLoad()
setupAnimation()
}
private func setupAnimation() {
let viewWidth:CGFloat = self.view.frame.width
let viewHeight:CGFloat = self.view.frame.height
//インスタンスを生成
for i in 0..<9 {
let imageView:UIImageView = UIImageView()
imageView.image = UIImage(named: "tax")
imageView.contentMode = .scaleAspectFit
imageView.frame.size = CGSize(width: 200, height: 200)
imageArray.append(imageView)
self.view.addSubview(imageArray[i])
}
//アニメーション
//1列目
imageArray[0].center = CGPoint(x: -viewWidth / 2, y: viewHeight * 0.6)
imageArray[1].center = CGPoint(x: 0, y: viewHeight * 0.3)
imageArray[2].center = CGPoint(x: viewWidth / 2, y: 0)
//2列目
imageArray[3].center = CGPoint(x: -viewWidth / 2, y: viewHeight * 1.2)
imageArray[4].center = CGPoint(x: 0, y: viewHeight * 0.9)
imageArray[5].center = CGPoint(x: viewWidth / 2, y: viewHeight * 0.6)
imageArray[6].center = CGPoint(x: viewWidth, y: viewHeight * 0.3)
//3列目
imageArray[7].center = CGPoint(x: viewWidth / 2, y: viewHeight * 1.2)
imageArray[8].center = CGPoint(x: viewWidth, y: viewHeight * 0.9)
//アニメーションの設定
//一定の速さで繰り返しのアニメーション表示を行う
UIView.animate(withDuration: 2, delay: 0.0, options: [.repeat, .curveLinear], animations: {
//1列目
self.imageArray[0].center = CGPoint(x: 0, y: viewHeight * 0.3)
self.imageArray[1].center = CGPoint(x: viewWidth / 2, y: 0)
self.imageArray[2].center = CGPoint(x: viewWidth, y: -viewHeight * 0.3)
//2列目
self.imageArray[3].center = CGPoint(x: 0, y: viewHeight * 0.9)
self.imageArray[4].center = CGPoint(x: viewWidth / 2, y: viewHeight * 0.6)
self.imageArray[5].center = CGPoint(x: viewWidth, y: viewHeight * 0.3)
self.imageArray[6].center = CGPoint(x: viewWidth * 1.5, y: 0)
//3列目
self.imageArray[7].center = CGPoint(x: viewWidth, y: viewHeight * 0.9)
self.imageArray[8].center = CGPoint(x: viewWidth * 1.5, y: viewHeight * 0.6)
},completion:nil)
}
}
☆参考文献