我想要一个像这样的闪光动画:https://gph.is/g/amWgbvj。(这是使用WebApp中的库:https://github.com/dvtng/react-loading-skeleton创建的)
我尝试使用带有不透明度的GradientLayer在所有子视图中创建闪光动画:
gradientLayer.colors = [Colors.tokenDark20.cgColor, Colors.tokenDark10.cgColor, Colors.tokenDark20.cgColor]
gradientLayer.opacity = 0.5但我得到了动画:https://gph.is/g/4L3K01P。
更多的努力:
我尝试使用库:https://github.com/gonzalonunez/Skeleton,尝试从左到右链接动画,但我不能为所有子视图设置相同长度的渐变形状:
extension ShimmerExampleCell: SkeletonOwner {
var gradientLayers: [CAGradientLayer] {
return [imagePlaceholderView.gradientLayer, titlePlaceholderView.gradientLayer, subtitlePlaceholderView.gradientLayer]
}
func slide(to dir: SkeletonDirection, group: ((CAAnimationGroup) -> Void) = { _ in }) {
imagePlaceholderView.gradientLayer.slide(to: .right) { (animationGroup) in
animationGroup.beginTime = 0
}
titlePlaceholderView.gradientLayer.slide(to: .right) { (animationGroup) in
animationGroup.beginTime = 1.1
subtitlePlaceholderView.gradientLayer.add(animationGroup, forKey: CAGradientLayer.kSlidingAnimationKey)
}
}
}我把动画放在这里:https://gph.is/g/ZPgPlXV
我处理闪光动画的方式是不是错了?
请帮帮我!提前谢谢你!
发布于 2021-06-02 18:51:35
视图控制器:
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var data: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
for value in 0..<20 {
self.data.append("\(value) data")
self.tableView.reloadData()
}
}ViewController的扩展:
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.labelName.text = data[indexPath.row]
return cell
}UITableViewCell:我在这里设置了SkeletonView的显示或隐藏状态
class TableViewCell: UITableViewCell {
@IBOutlet weak var labelName: UILabel!
@IBOutlet weak var imgView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let views = [labelName, imgView]
views.forEach {$0?.showHideSkeletonView(show: true)}
DispatchQueue.main.asyncAfter(deadline: .now()+3) {
views.forEach {$0?.showHideSkeletonView(show: false)}
}
}ConfigSkeleton:在这里我为我的skeletonView添加了一个设置,动画和颜色的函数。
import SkeletonView
extension UIView {
func setSkeletonView() {
self.isSkeletonable = true
}
func showHideSkeletonView(show: Bool) {
if show {
let gradient = SkeletonGradient(baseColor: UIColor.clouds)
let animation = SkeletonAnimationBuilder().makeSlidingAnimation(withDirection: .topLeftBottomRight)
self.showAnimatedGradientSkeleton(usingGradient: gradient, animation: animation)
} else {
self.hideSkeleton()
}
}发布于 2020-10-05 16:40:10
您可以尝试使用库SkeletonView;它可以帮助您轻松地在想要的任何地方实现微光动画。
https://stackoverflow.com/questions/64203737
复制相似问题