Created
November 4, 2022 04:06
-
-
Save woodycatliu/46eb582a9aba148b008ff407e2a0120e to your computer and use it in GitHub Desktop.
The simple extension for UIKit animate in RxSwift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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