Skip to content

Commit

Permalink
Merge pull request #4 from sjc-bui/fade-animation
Browse files Browse the repository at this point in the history
update animation
  • Loading branch information
sjc-bui committed May 25, 2021
2 parents 67d8dd3 fd25c20 commit a08581f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion QBIndicatorButton.podspec
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
Pod::Spec.new do |s|
s.name = 'QBIndicatorButton'
s.version = '0.1.4'
s.version = '0.1.5'
s.summary = 'Custom of UIButton in Swift'

s.description = <<-DESC
Expand Down
42 changes: 24 additions & 18 deletions QBIndicatorButton/Classes/QBIndicatorButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 206,26 @@ open class QBIndicatorButton: UIButton {
/// Display activity indicator inside the button
/// - Parameter completion: The completion handler
open func start(_ completion: ActionCompletionHandler = nil) {
guard !isLoading else { return }

if activityIndicator == nil {
activityIndicator = createActivityIndicator()
}

self.isUserInteractionEnabled = false
self.isLoading = true

activityIndicator.isUserInteractionEnabled = false
let indicatorWidth = activityIndicator.frame.width != 0 ?
activityIndicator.frame.width : 20
self.padding = (self.frame.height - indicatorWidth) / 2

UIView.animate(withDuration: animatedScaleDuration) {
self.transform = CGAffineTransform(scaleX: self.animatedScale, y: self.animatedScale)
} completion: { done in
} completion: { _ in
UIView.animate(withDuration: self.animatedScaleDuration) {
self.transform = CGAffineTransform.identity
} completion: { done in
} completion: { _ in
UIView.transition(with: self, duration: self.titleFadeDuration, options: .curveEaseOut) {
self.alpha = 0.8
self.titleLabel?.alpha = self.indicatorPosition == .center ? 0.0 : 0.6
Expand All @@ -237,31 240,30 @@ open class QBIndicatorButton: UIButton {
/// Hide activity indicator inside the button
/// - Parameter completion: The completion handler
open func stop(_ completion: ActionCompletionHandler = nil) {
DispatchQueue.main.async { [weak self] in
guard let self = self,
self.activityIndicator != nil else {
return
}
guard isLoading else { return }

self.isUserInteractionEnabled = true
self.isLoading = false

UIView.animate(withDuration: 0.2) {
self.activityIndicator.alpha = 0
} completion: { _ in
self.activityIndicator.stopAnimating()
self.isUserInteractionEnabled = true
self.isLoading = false
self.activityIndicator.removeFromSuperview()
}

UIView.transition(with: self,
duration: self.titleFadeDuration,
options: .curveEaseOut) {
self.alpha = 1.0
self.titleLabel?.alpha = 1.0
} completion: { _ in
completion?()
}
UIView.transition(with: self,
duration: self.titleFadeDuration,
options: .curveEaseOut) {
self.alpha = 1.0
self.titleLabel?.alpha = 1.0
} completion: { _ in
completion?()
}
}

private func createActivityIndicator() -> UIActivityIndicatorView {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.hidesWhenStopped = true
activityIndicator.color = activityIndicatorColor
return activityIndicator
}
Expand All @@ -271,6 273,10 @@ open class QBIndicatorButton: UIButton {
self.addSubview(activityIndicator)
positionActivityIndicator()
activityIndicator.startAnimating()
activityIndicator.alpha = 0
UIView.animate(withDuration: 0.2) {
self.activityIndicator.alpha = 1
}
}

private func positionActivityIndicator() {
Expand Down

0 comments on commit a08581f

Please sign in to comment.