UICollectionView动画不显示数据

时间:2018-05-16 07:24:05

标签: ios swift animation uicollectionview

我想将动画添加到集合视图中,列表非常缓慢地上下移动,在集合视图重新加载数据后我启动计时器并添加重复动画的方法,有我的代码,动画行为非常在动画期间,我丢失了列表数据。 有我的代码怎么解决这个? 谢谢你的回复。

 fileprivate func startTimer() {

    if timer == nil {

      timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(addAnimationToCollection)), userInfo: nil, repeats: true)
    }

  }
  func scrollToIndexPath(path: NSIndexPath) {
    let atts = self.collectionView!.layoutAttributesForItem(at: path as IndexPath)
    self.endPoint = CGPoint(x: 0, y: atts!.frame.origin.y - self.collectionView.contentInset.top)
    self.scrollPoint = self.collectionView!.contentOffset
    self.scrollingUp = self.collectionView!.contentOffset.y > self.endPoint!.y

    self.scrollTimer?.invalidate()
    self.scrollTimer = Timer.scheduledTimer(timeInterval:0.01, target: self, selector: (#selector(scrollTimerTriggered(timer:))), userInfo: nil, repeats: true)
  }

  func scrollTimerTriggered(timer: Timer) {

    let dif = fabs(self.scrollPoint!.y - self.endPoint!.y) / 10000.0
    let modifier: CGFloat = self.scrollingUp ? -2 : 2

    self.scrollPoint = CGPoint(x: self.scrollPoint!.x, y: self.scrollPoint!.y + (modifier * dif))
    self.collectionView?.contentOffset = self.scrollPoint!
    let maximumOffset = self.collectionView.contentSize.height - self.collectionView.frame.size.height
    if self.scrollingUp && self.collectionView!.contentOffset.y < maximumOffset {
      self.collectionView!.contentOffset = self.endPoint!
      timer.invalidate()
    } else if !self.scrollingUp && self.collectionView!.contentOffset.y >= self.endPoint!.y {
      self.collectionView!.contentOffset = self.endPoint!
      timer.invalidate()
    }

  }
  fileprivate func invelidTimer() {

    if timer != nil {
      timer?.invalidate()
      timer = nil
    }
    if scrollTimer != nil {
      scrollTimer?.invalidate()
      scrollTimer = nil
    }
  }
  @objc func  addAnimationToCollection() {

        let currentOffset = self.collectionView.contentOffset.y
        let maximumOffset = self.collectionView.contentSize.height - self.collectionView.frame.size.height
        if maximumOffset - currentOffset < 0 {
             let first : NSIndexPath = NSIndexPath(row: 0, section: 0)
          self.scrollToIndexPath(path: first)
        }
        else {
            let last : NSIndexPath = NSIndexPath(row: self.collectionList.count - 1, section: 0)
          self.scrollToIndexPath(path: last)

        }

  }

0 个答案:

没有答案