我正在尝试在集合视图中将单元格连续移动到随机点。 我在下面创建了函数,但它的动作很奇怪,并且didSelectItemAt方法有时不会被调用。 有什么方法可以使动画更加平滑,并在用户点击单元格时仍然触发didSelectItemAt方法。
func randomPointAnimation(start: CGPoint, view: UICollectionViewCell ,collectionView: UICollectionView) {
var toX: CGFloat = 0.0
var toY: CGFloat = 0.0
let backView = collectionView
toX = randomXPoint(point: start.x, view: backView)
toY = randomYPoint(point: start.y, view: backView)
let randomTo = CGPoint(x: toX, y: toY)
let control1 = randomControlPoint(x1: start, x2: randomTo)
let control2 = randomControlPoint(x1: start, x2: randomTo)
let path = createCurvePath(start: start, to: randomTo, control1: CGPoint(x: control1.x, y: control1.y), control2: CGPoint(x: control2.x, y: control2.y))
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 1.0
self.view.layer.addSublayer(shapeLayer)
let moveAlongPath = CAKeyframeAnimation(keyPath: "position")
let curvePath = path
moveAlongPath.path = curvePath.cgPath
moveAlongPath.duration = 3
moveAlongPath.calculationMode = CAAnimationCalculationMode.paced
moveAlongPath.timingFunctions = [CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)]
CATransaction.begin()
CATransaction.setCompletionBlock({
self.randomPointAnimation(start: randomTo, view: view, collectionView: backView)
})
let layer = createLayer(view: view)
layer.add(moveAlongPath, forKey: "animate along Path")
CATransaction.commit()
}