LoginSignup
1
1

More than 5 years have passed since last update.

【Swift3】2つのUIView.animateを交互に動かす

Last updated at Posted at 2017-01-10

アニメーションで心拍を表現したい!

ハートを心拍のようにドキドキ、ドゥンドゥンと動かしたい。
ライブラリで何とかなりそうな気はしたけど、デフォルトで入っているアニメーションでどうにかなった。

やりたかったこと

UIImageViewを
0.5秒かけて小さく
→2.0秒かけて大きく
 するのをループする。

capture.gif

二つのアニメーションに分けないとだめなのでoptions: .repeatが使えなかった。

完成したやつ

AnimationController.swift
let image = UIImage(named: "image.png")
override func viewDidAppear(_ animated: Bool) {
    animate2()
}

func animate1() {
    UIView.animate(withDuration: 2.0, animations: {
        self.image.transform = CGAffineTransform(scaleX: 1.5, y: 1.5
    }) { _ in
        self.animate2()
    }
}

func animate2() {
    UIView.animate(withDuration: 0.5, animations: {
        self.image.transform = CGAffineTransform(scaleX: 1.0 / 1.5, y: 1.0 / 1.5)
    }) { _ in
        self.animate1()
    }
}

completionにお互いのアニメーションを指定してゴリゴリっと。
.repeatを使わなくても何とかできました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1