우선 애니메이션은 Closure 기반으로 작성한다.
Animate가 실행되는 동안에는 user Information (터치 등)이 일시적으로 disabled 되었다가, 끝나면 다시 enable
animate(withDuration:animations:) | Apple Developer Documentation
<aside>
💡 Animate changes to one or more views using the specified duration.
</aside>
class func animate(withDuration duration: TimeInterval,
delay: TimeInterval,
options: UIView.AnimationOptions = [],
animations: @escaping () -> Void,
completion: ((Bool) -> Void)? = nil)
**Duration
**애니메이션이 동작하는 시간 단위는 초이고,
→ 예를 들어 withDuration : 1.0
이면 애니메이션이 1초동안 동작한다.
Delay
: 딜레이를 가진 다음에 애니메이션이 동작함. 초단위
Options
: 애니메이션에 다양한 옵션을 줄 수 있다.
.allowUserInteration | 애니메이션 도중 터치 활성화 |
---|---|
.repeat | 무한 반복 |
.autoreverse | 반대로 실행 |
.curveEaseInOut | |
.curveEaseIn | |
.curveEaseOut | 애니메이팅의 속도를 지정할 수 있는 옵션 |
Animations
: 실제로 애니메이션이 될 부분
Completion
: Duration 동안의 애니메이션이 끝나고 나서 동작하는 구간이다.
hidden과 같이 중간값 계산이 불가능한 속성은 적용되지 않는다.
클로저 내부에서 레이아웃에 접근해서 constant 값을 직접 바꿔주는 경우에는 애니메이션이 적용되지 않을 수 있음
⇒ layoutIfNeeded()
를 사용하여 Update 해주자!
@objc func buttonDidTap() {
self.animationTargetView.snp.remakeConstraints {
$0.top.equalTo(self.view.safeAreaLayoutGuide).inset(16)
$0.centerX.equalToSuperview()
$0.size.equalTo(240)
}
UIView.animate(
withDuration: 3,
delay: 3,
animations: {
self.animationTargetView.layoutIfNeeded()
}
)
}