首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS中制作类似react-loading- Shimmer的Shimmer动画

如何在iOS中制作类似react-loading- Shimmer的Shimmer动画
EN

Stack Overflow用户
提问于 2020-10-05 14:57:09
回答 2查看 613关注 0票数 0

我想要一个像这样的闪光动画:https://gph.is/g/amWgbvj。(这是使用WebApp中的库:https://github.com/dvtng/react-loading-skeleton创建的)

我尝试使用带有不透明度的GradientLayer在所有子视图中创建闪光动画:

代码语言:javascript
复制
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,尝试从左到右链接动画,但我不能为所有子视图设置相同长度的渐变形状:

代码语言:javascript
复制
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

我处理闪光动画的方式是不是错了?

请帮帮我!提前谢谢你!

EN

回答 2

Stack Overflow用户

发布于 2021-06-02 18:51:35

视图控制器:

代码语言:javascript
复制
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的扩展:

代码语言:javascript
复制
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的显示或隐藏状态

代码语言:javascript
复制
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添加了一个设置,动画和颜色的函数。

代码语言:javascript
复制
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()
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2020-10-05 16:40:10

您可以尝试使用库SkeletonView;它可以帮助您轻松地在想要的任何地方实现微光动画。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64203737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档