我有一个带有9个像元的3x3集合视图,我创建了一个动画功能来将像元层移动到视图中的随机点。 我正在尝试检测用户在特定单元格上点击时的点击手势,但是didSelectItemAt和touchesBegan不会触发。有什么方法可以检测到哪个单元格被窃听了吗?还是不应该通过路径移动细胞层? 上面的代码是我的动画功能
var toX: CGFloat = 0.0
var toY: CGFloat = 0.0
let inset = (self.view.frame.width - collectionView.frame.width) / 2
let backView = collectionView
toX = CGFloat.random(in: inset...inset + collectionView.frame.width - view.frame.width)
toY = CGFloat.random(in: 0.0...collectionView.frame.height - view.frame.height)
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.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()