Sie die Geschwindigkeit Eigenschaft der Ebene festlegen (wie in [Rotoavas Antwort]) (https://stackoverflow.com/a/23146861/5271393), um die Geschwindigkeit der Animation zu ändern. Das Problem besteht darin, dass Sie willkürliche Werte verwenden, da Sie die tatsächliche Dauer der Einfügungsanimation nicht kennen.
Mit this post können Sie herausfinden, was die Standardanimationsdauer ist.
newAnimationDuration = (1/layer.speed)*originalAnimationDuration
layer.speed = originalAnimationDuration/newAnimationDuration
Wenn Sie die Animation 400ms lang, in Ihrem Layout würden Sie machen wollte:
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* attributes = [super finalLayoutAttributesForDisappearingItemAtIndexPath:indexPath];
//set attributes here
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
CGFloat originalAnimationDuration = [CATransaction animationDuration];
CGFloat newAnimationDuration = 0.4f;
cell.layer.speed = originalAnimationDuration/newAnimationDuration;
return attributes;
}
In meinem Fall hatte ich Zellen, die gezogen werden könnte außerhalb des Bildschirms, und ich wollte ändern, um die Dauer der die Löschanimation basierend auf der Geschwindigkeit der Schwenkgeste.
Im Gestenerkenner (der Teil Ihrer Sammlung Ansicht sein soll):
- (void)handlePanGesture:(UIPanGestureRecognizer *)sender
{
CGPoint dragVelocityVector = [sender velocityInView:self.collectionView];
CGFloat dragVelocity = sqrt(dragVelocityVector.x*dragVelocityVector.x + dragVelocityVector.y*dragVelocityVector.y);
switch (sender.state) {
...
case UIGestureRecognizerStateChanged:{
CustomLayoutClass *layout = (CustomLayoutClass *)self.collectionViewLayout;
layout.dragSpeed = fabs(dragVelocity);
...
}
...
}
Dann in Ihrem customLayout:
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* attributes = [super finalLayoutAttributesForDisappearingItemAtIndexPath:indexPath];
CGFloat animationDistance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
CGFloat originalAnimationDuration = [CATransaction animationDuration];
CGFloat newAnimationDuration = animationDistance/self.dragSpeed;
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.layer.speed = originalAnimationDuration/newAnimationDuration;
return attributes;
}
der Dokumentation von Apple Laut „Wenn Layoutänderungen animieren, werden die Animation Timing und Parameter, die von der Sammlung Ansicht gesteuert.“ Dies bezieht sich auf die setCollectionView: animated: -Methode, aber ich vermute, dass dies auch für das Ändern der Grenzen der Sammlungsansicht gilt. Sorry, ich kann nicht mehr Hilfe sein, ich stecke auf dem gleichen Problem fest. Ich vermute, dass die Antwort irgendwo innerhalb des UICollectionView-Objekts selbst liegt. – Ash