菜鸟教程小白 发表于 2022-12-11 17:13:10

ios - 在单元格中为 ProgressView 设置动画


                                            <p><p>我想在 CollectionViewCell 中添加一个 progressView。 ProgressView 应该是动画的,并在 10 秒内运行 von 1 到 0 的进度。 </p>

<p>所以现在对每个进度 View 都使用“viewWithTag”,并更新我的进度:</p>

<pre><code>UIView.animateWithDuration(3, animations: { () -&gt; Void in
    progressView.setProgress(1.0, animated: true)
})
</code></pre>

<p>但是,我想它必须容易得多。当生病重新加载单元格时,我想再次为进度设置动画。那么直接将该功能包含在我的单元格中是否应该更容易?</p>

<p>喜欢:</p>

<pre><code>override func prepareForReuse() {
    super.prepareForReuse()
    print(&#34;prepareForReuse&#34;)
    UIView.animateWithDuration(3, animations: { () -&gt; Void in
       progressView.setProgress(1.0, animated: true)
    })
}
</code></pre>

<p>但这不起作用,滚动时也应该是一个问题。有没有办法让单元格中的 progressView 动画精确 10 秒,并且(当你滚动时)progressView 仍然没有从头开始? </p>

<p>好的,当我更新我的单元格时,我现在开始我的动画:</p>

<pre><code>func updateCell() {

    photos.comment = &#34;dsadhjsa hdjksahdjsahfk jhasfjklha fjkhdjsk fhjksdf hjskdfh jkdshfjksdh fdhsjkfhdsjkf dsadsad dsad sad &#34; // test Test

    let h: Int = random() % 100;

    photos.height = 120 + CGFloat(h) // set custom height for test

    let path = NSIndexPath(forItem: 2, inSection: 0)

    self.collectionViewLayout.invalidateLayout()

    collectionView?.reloadItemsAtIndexPaths()

    if let progressView = view.viewWithTag(2) as? UIProgressView {

       progressView.setProgress(0, animated: false)

       UIView.animateWithDuration(4, animations: { () -&gt; Void in
       progressView.layoutIfNeeded()
       progressView.setProgress(1, animated: true)

})

}

}
</code></pre>

<p>这是有效的,但是(当然)当我向下滚动时,我的单元格会丢失动画。我想这将是存储当前进度值的更好方法,如果单元格再次可见,我将再次加载它?</p>

<p>或者还有其他可能性吗? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>override func prepareForReuse() {
super.prepareForReuse()
print(&#34;prepareForReuse&#34;)
UIView.animateWithDuration(3, animations: { () -&gt; Void in
dispatch_async(dispatch_get_main_queue()) { in
progressView.setProgress(1.0, animated: true)
   }
    })
}
</code></pre>

<p>试试这个。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在单元格中为 ProgressView 设置动画,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38848146/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38848146/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在单元格中为 ProgressView 设置动画