Ios 使用UIPangestureRecognitor以给定速度滚动UICollectionView

Ios 使用UIPangestureRecognitor以给定速度滚动UICollectionView,ios,scroll,uicollectionview,uiscrollview,Ios,Scroll,Uicollectionview,Uiscrollview,我正在尝试实现一种与股票iOS照片应用程序类似的照片选择方法。这使用uipangestureerecognizer工作,该识别器禁用UICollectionView上的滚动。在stock photos应用程序中,当平移到达屏幕的顶部或底部时,滚动视图开始以给定的速度滚动,并且越靠近屏幕边缘,滚动越快 在uicrollview或UICollectionView中,似乎没有API可以以给定的速度滚动。是否有任何聪明的方法可以使用现有的方法(如scrollToVisibleRect或在动画块中设置内容

我正在尝试实现一种与股票iOS照片应用程序类似的照片选择方法。这使用
uipangestureerecognizer
工作,该识别器禁用
UICollectionView
上的滚动。在stock photos应用程序中,当平移到达屏幕的顶部或底部时,滚动视图开始以给定的速度滚动,并且越靠近屏幕边缘,滚动越快


uicrollview
UICollectionView
中,似乎没有API可以以给定的速度滚动。是否有任何聪明的方法可以使用现有的方法(如
scrollToVisibleRect
或在动画块中设置内容偏移)来实现这一点?我担心,虽然这些可能会起作用,但它们的运动会很不平稳

我最终为此创建了一个
SpeedScrollableCollectionViewController
,并使用
UIPangestureRecognitor
对其进行控制(与问题无关)

class SpeedScrollableCollectionViewController:UICollectionViewController{
private var lastFrameTime:CFTimeInterval?
私有变量显示链接:CADisplayLink?
重写初始化(collectionViewLayout布局:UICollectionViewLayout){
super.init(collectionViewLayout:layout)
}
重写init(nibName-nibNameOrNil:String?,bundle-nibBundleOrNil:bundle?){
super.init(nibName:nibNameOrNil,bundle:nibBundleOrNil)
}
必需初始化?(编码器:NSCoder){
super.init(编码器:编码器)
}
变量滚动速度:CGPoint=.0{
迪塞特{
保护滚动速度!=.0其他{
displayLink?.isPaused=true
返回
}
guard displayLink==nil else{
lastFrameTime=CACurrentMediaTime()
displayLink?.isPaused=false
返回
}
lastFrameTime=CACurrentMediaTime()
displayLink=CADisplayLink(目标:self,选择器:#选择器(updateContentOffset))
displayLink?.add(添加到:.main,forMode:.common)
}
}
@objc func updateContentOfffset(){
推迟{
lastFrameTime=CACurrentMediaTime()
}
guard let lastFrameTime=lastFrameTime else{return}
设dt=CACurrentMediaTime()-lastFrameTime
设dx=scrollSpeed.x*CGFloat(dt)
设dy=scrollSpeed.y*CGFloat(dt)
var currentOffset=collectionView.contentOffset
currentOffset.x+=dx
currentOffset.y+=dy
collectionView.setContentOffset(currentOffset,动画:false)
}
}

我最终为此创建了一个
SpeedScrollableCollectionViewController
,并使用
UIPangestureRecognitor对其进行控制(与问题无关

class SpeedScrollableCollectionViewController:UICollectionViewController{
private var lastFrameTime:CFTimeInterval?
私有变量显示链接:CADisplayLink?
重写初始化(collectionViewLayout布局:UICollectionViewLayout){
super.init(collectionViewLayout:layout)
}
重写init(nibName-nibNameOrNil:String?,bundle-nibBundleOrNil:bundle?){
super.init(nibName:nibNameOrNil,bundle:nibBundleOrNil)
}
必需初始化?(编码器:NSCoder){
super.init(编码器:编码器)
}
变量滚动速度:CGPoint=.0{
迪塞特{
保护滚动速度!=.0其他{
displayLink?.isPaused=true
返回
}
guard displayLink==nil else{
lastFrameTime=CACurrentMediaTime()
displayLink?.isPaused=false
返回
}
lastFrameTime=CACurrentMediaTime()
displayLink=CADisplayLink(目标:self,选择器:#选择器(updateContentOffset))
displayLink?.add(添加到:.main,forMode:.common)
}
}
@objc func updateContentOfffset(){
推迟{
lastFrameTime=CACurrentMediaTime()
}
guard let lastFrameTime=lastFrameTime else{return}
设dt=CACurrentMediaTime()-lastFrameTime
设dx=scrollSpeed.x*CGFloat(dt)
设dy=scrollSpeed.y*CGFloat(dt)
var currentOffset=collectionView.contentOffset
currentOffset.x+=dx
currentOffset.y+=dy
collectionView.setContentOffset(currentOffset,动画:false)
}
}