更改收藏视图后,开始播放乐透



我对彩票动画有问题,我的应用程序上有一些入门功能,我想实现的是,每当collectionview中的视图发生更改时,我都会启动我的动画,我有4个页面和4个不同的彩票动画。目前,如果我调用animation.play((函数,一旦应用程序启动,我所有的动画都会同时播放,所以一旦我到达最后一页,动画就结束了。我希望我的彩票只玩一次,当视图显示时。

这是我的手机

class IntroductionCollectionViewCell: UICollectionViewCell {
@IBOutlet var title: UILabel!
@IBOutlet var subtitleDescription: UILabel!
@IBOutlet var animationView: AnimationView!
override func awakeFromNib() {
super.awakeFromNib()
}

public func configure(with data: IntroInformations) {
let animation = Animation.named(data.animationName)

title.text = data.title
subtitleDescription.text = data.description
animationView.animation = animation
}
static func nib() -> UINib {
return UINib(nibName: "IntroductionCollectionViewCell", bundle: nil)
}
}

这就是我的收藏视图的设置方式

extension IntroductionViewController {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
pages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "IntroductionCollectionViewCell", for: indexPath) as! IntroductionCollectionViewCell

cell.configure(with: pages[indexPath.row])
cell.animationView.loopMode = .loop
cell.animationView.play()

return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let scrollPos = scrollView.contentOffset.x / view.frame.width
self.pageControl.currentPage = Int(floorf(Float(scrollPos)))

let n = pageControl.numberOfPages

if self.pageControl.currentPage == n - 1 {
continueButton.isHidden = false
} else {
continueButton.isHidden = true
}
}
}

提前感谢!!

您可以使用collectionViewDelegate willDisplay和didEndDisplaying方法来启动/停止动画。而不是在配置单元格时。-https://developer.apple.com/documentation/uikit/uicollectionviewdelegate如果希望动画只运行一次,请不要使用循环选项。

if let cell = cell as? IntroductionCollectionViewCell {
cell.animationView.loopMode = .playOnce
cell.animationView.play()
}

这是答案,我需要检查一下是不是单元格加载了我的单元格中的乐透动画是

scrollViewDidEndScrollingAnimation在UICollectionViewDelegateFlowLayout上使用此方法,并实现暂停或播放彩票的功能。

希望这会有所帮助。

最新更新