-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a shimmering card view when loading card art #6355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // | ||
| // ShimmerView.swift | ||
| // StripePaymentSheet | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| /// A view that displays a sliding shimmer animation overlay. | ||
| /// Used as a loading indicator while card art images are downloaded. | ||
| class ShimmerView: UIView { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add some snapshot tests for this view?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't consider it due to the animation. We could write some unit tests that test to make sure the gradient has various alpha components, but that doesn't seem too valuable? |
||
| private let gradientLayer = CAGradientLayer() | ||
|
|
||
| override init(frame: CGRect) { | ||
| super.init(frame: frame) | ||
| isUserInteractionEnabled = false | ||
| isAccessibilityElement = false | ||
| clipsToBounds = true | ||
| setupGradient() | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| override func layoutSubviews() { | ||
| super.layoutSubviews() | ||
| gradientLayer.frame = bounds | ||
| } | ||
|
|
||
| private func setupGradient() { | ||
| gradientLayer.colors = [ | ||
| UIColor.white.withAlphaComponent(0.0).cgColor, | ||
| UIColor.white.withAlphaComponent(0.4).cgColor, | ||
| UIColor.white.withAlphaComponent(0.0).cgColor, | ||
| ] | ||
| gradientLayer.startPoint = CGPoint(x: 0, y: 0.5) | ||
| gradientLayer.endPoint = CGPoint(x: 1, y: 0.5) | ||
| gradientLayer.locations = [-0.5, -0.25, 0.0] | ||
| layer.addSublayer(gradientLayer) | ||
|
|
||
| let animation = CABasicAnimation(keyPath: "locations") | ||
| animation.fromValue = [-0.5, -0.25, 0.0] | ||
| animation.toValue = [1.0, 1.25, 1.5] | ||
| animation.duration = 1.2 | ||
| animation.repeatCount = .infinity | ||
| gradientLayer.add(animation, forKey: "shimmer") | ||
| } | ||
|
|
||
| func stopShimmering() { | ||
| gradientLayer.removeAllAnimations() | ||
| removeFromSuperview() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to remove shimmer on line 376?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 376 is showing me:
Removing the shimmer as proposed in the PR (on line 371) seems appropriate since we've added it prior to loading, and we should be removing it irregardless of the card art image loads or we fallback to the network logo.