Making an animation to expand and shrink an UIView

后端 未结 3 793
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 14:49

I want to create an animation that will resize an UIView and its contents by a factor. Basically, I want to make an animation that first expands the view then shrinks it bac

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 15:15

    Swift 5 UIView extension:

    extension UIView {
        func pulse(withIntensity intensity: CGFloat, withDuration duration: Double, loop: Bool) {
            UIView.animate(withDuration: duration, delay: 0, options: [.repeat, .autoreverse], animations: {
                loop ? nil : UIView.setAnimationRepeatCount(1)
                self.transform = CGAffineTransform(scaleX: intensity, y: intensity)
            }) { (true) in
                self.transform = CGAffineTransform.identity
            }
        }
    }
    

    And then use the following:

    yourView.pulse(withIntensity: 1.2, withDuration: 0.5, loop: true)
    

    Just make sure you replace yourView with your own view.

提交回复
热议问题