Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Created November 4, 2022 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/46eb582a9aba148b008ff407e2a0120e to your computer and use it in GitHub Desktop.
Save woodycatliu/46eb582a9aba148b008ff407e2a0120e to your computer and use it in GitHub Desktop.
The simple extension for UIKit animate in RxSwift
import UIKit
import RxSwift
import RxCocoa
struct WRxAnimation {
let view: UIView
let transform: CGAffineTransform
}
extension Reactive where Base: UIView {
func animate(withDuration duration: TimeInterval, delay: TimeInterval = 0, options: UIView.AnimationOptions = [], _ transform: CGAffineTransform) -> Observable<WRxAnimation> {
return Observable.create { obser in
let newAnimation = base.transform.concatenating(transform)
UIView.animate(withDuration: duration, delay: delay, options: options, animations: {
self.base.transform = newAnimation
}, completion: { _ in
obser.onNext(WRxAnimation(view: self.base, transform: transform))
obser.onCompleted()
})
return Disposables.create()
}
}
func fade(withDuration duration: TimeInterval, delay: TimeInterval = 0) -> Observable<WRxAnimation> {
return Observable.create { obser in
UIView.animate(withDuration: duration, delay: delay, animations: {
self.base.alpha = 0
}, completion: { _ in
obser.onNext(WRxAnimation(view: self.base, transform: self.base.transform))
obser.onCompleted()
})
return Disposables.create()
}
}
}
extension ObservableType where E == WRxAnimation {
func nextAnimate(withDuration duration: TimeInterval, delay: TimeInterval = 0, _ transform: CGAffineTransform) -> Observable<WRxAnimation> {
return flatMap { animation -> Observable<AFRxAnimation> in
let newTransform = animation.transform.concatenating(transform)
return animation.view.rx.animate(withDuration: duration, delay: delay, newTransform)
}
}
func fade(withDuration duration: TimeInterval, delay: TimeInterval = 0) -> Observable<WRxAnimation> {
return flatMap {
return $0.view.rx.fade(withDuration: duration, delay: delay)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment