Ios UICollectionView动画没有';不显示数据
我想将动画添加到集合视图中,该列表上下移动非常缓慢,在集合视图重新加载数据后,我启动计时器并添加重复动画的方法,这是我的代码,动画行为非常不寻常,在动画过程中我丢失了列表数据。 有我的代码怎么能解决这个问题? 谢谢你的回复Ios UICollectionView动画没有';不显示数据,ios,swift,animation,uicollectionview,Ios,Swift,Animation,Uicollectionview,我想将动画添加到集合视图中,该列表上下移动非常缓慢,在集合视图重新加载数据后,我启动计时器并添加重复动画的方法,这是我的代码,动画行为非常不寻常,在动画过程中我丢失了列表数据。 有我的代码怎么能解决这个问题? 谢谢你的回复 fileprivate func startTimer() { if timer == nil { timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#sel
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)
}
}
fileprivate func startTimer(){
如果计时器==nil{
timer=timer.scheduledTimer(时间间隔:1,目标:self,选择器:(#选择器(addAnimationToCollection)),userInfo:nil,repeats:true)
}
}
func scrollToIndexPath(路径:NSIndexPath){
设atts=self.collectionView!.layouttributesforItem(在:路径为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(时间间隔:0.01,目标:self,选择器:(#选择器(scrollTimerTriggered(计时器:)),用户信息:nil,重复:true)
}
func scrollTimerTriggered(计时器:计时器){
设dif=fabs(self.scrollPoint!.y-self.endPoint!.y)/10000.0
让修饰符:CGFloat=self.scrollingUp?-2:2
self.scrollPoint=CGPoint(x:self.scrollPoint!.x,y:self.scrollPoint!.y+(修饰符*dif))
self.collectionView?.contentOffset=self.scrollPoint!
设maximumOffset=self.collectionView.contentSize.height-self.collectionView.frame.size.height
如果self.scrollingUp&&self.collectionView!.contentOffset.y=self.endPoint!.y{
self.collectionView!.contentOffset=self.endPoint!
timer.invalidate()
}
}
fileprivate func invelidTimer(){
如果计时器!=零{
计时器?.invalidate()
计时器=零
}
如果scrollTimer!=nil{
scrollTimer?.invalidate()
滚动计时器=零
}
}
@objc func addAnimationToCollection(){
让currentOffset=self.collectionView.contentOffset.y
设maximumOffset=self.collectionView.contentSize.height-self.collectionView.frame.size.height
如果maximumOffset-currentOffset<0{
让我们先:nsindepath=nsindepath(行:0,节:0)
scrollToIndexPath(路径:first)
}
否则{
let last:nsindepath=nsindepath(行:self.collectionList.count-1,节:0)
scrollToIndexPath(路径:last)
}
}
最好在没有计时器的willDisplayCell中制作动画。在我看来,您可能不需要UIView.animate
,因为“.scrollToItem”已经完成了动画部分。此外,您可能试图滚动到尚未填充的项目yet@GaneshSomani我希望动画制作速度非常慢,但scrollToItem动画制作速度非常快。@SharadChauhan我尝试了你的建议,但这不是我想要的。@GaneshSomani我正在编辑我的问题并改进我的代码。现在集合视图慢慢滚动到列表的下方,但我仍然有一个缓慢滚动到顶部的问题。