27

我正在尝试从上到下制作视图幻灯片。这没什么大不了的,我用CABasicAnimation这个。问题是当我想删除视图时。我用这个动画。

CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDelegate:self];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.layer.position.x, 0 - self.view.bounds.size.height / 2)];
animation.fromValue = [NSValue valueWithCGPoint:self.view.layer.position];
animation.autoreverses = NO;
animation.repeatCount = 0;
animation.duration = 0.25;
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
[self.view.layer  addAnimation:animation forKey:@"moveX"];

完美地为视图设置动画。但是,动画结束后,我的视图又出现了。所以我添加了这一行:

[self.view removeFromSuperview];

这会删除视图,但没有动画。所以我决定将删除代码添加到这个委托:

-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag

所以现在,动画起作用了,视图消失了,但有时,我可以看到视图出现和消失的速度更快,就像动画之后,视图出现,然后animationDidStop调用委托,视图消失,显然这很糟糕。我究竟做错了什么?

4

6 回答 6

66

可能要设置这些属性。它们导致演示文稿在动画结束时保留。

animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;

Then the "animationDidStop:" method can be used to remove the view at the end of the animation:

-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag {
    if (animation == [containerView.layer animationForKey:@"moveX"]) {
        // remove view here, add another view and/or start another transition
    }
}
于 2008-10-30T16:05:38.563 回答
15

好吧,根据 Apple 示例“MoveMe”,这个(removedOnCompletion)应该可以工作,但是,它似乎没有。

因此,在您的代码之后添加这些行:

[self.view.layer  addAnimation:animation forKey:@"moveX"];
self.view.layer.position = [animation.toValue CGPointValue];

这确保了动画运行后,图层被正确定位。

于 2008-10-22T22:41:15.470 回答
3

I had this issue when performing several animations in an animation group. I had to set a couple properties on the animation group itself, not the individual animations.

CAAnimationGroup *animGroup = [CAAnimationGroup animation];

// MAKE SURE YOU HAVE THESE TWO LINES.
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;

animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, nil];
animGroup.duration = tAnimationDuration;
[tImageView.layer addAnimation:animGroup forKey:nil];
于 2013-02-25T20:02:23.563 回答
0

按照 Rob 的建议将视图设置为隐藏应该这样做。

对于属性的属性,我会坚持使用 ObjC 2.0 样式,就像您在代码中已有的那样。

set.view.hidden = YES;
于 2008-10-22T17:04:48.440 回答
0

这个也咬了我一口。您想将动画的 removedOnCompletion 标志设置为 NO。它默认为 YES,这意味着在动画完成后,它被移除,并且视图恢复到其初始状态。

于 2008-10-22T17:31:43.877 回答
-2

您可以将视图的隐藏属性设置为“是”吗?

我认为会是:

self.view.hidden = YES;

但它可能是:

[self.view setHidden:YES];

事实证明,我在找出访问属性的正确方法方面相当蹩脚。

于 2008-10-22T16:46:16.760 回答