动画后,查看位置重置 [英] After Animation, View position resets

查看:95
本文介绍了动画后,查看位置重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让视图从上到下滑动。这不是什么大不了的,我使用 CABasicAnimation 。问题是当我想删除视图。我使用这个动画。

I am trying to make a view slide from top to bottom. This is not a big deal, I used CABasicAnimation for this. The problem is when I want to remove the view. I use this animation.

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"];

这完全动画了视图。但是,动画完成后,我的视图再次出现。所以我添加了这一行:

Which animates the view perfectly. But, after the animation finishes, my view appears again. So I added this line :

[self.view removeFromSuperview];

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

Which removes the view, but with no animation. So I decided to add the remove code to this delegate:

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

现在,动画工作,视图消失,但有时候,视图出现和消失得更快,就像后面的动画,视图出现,然后调用 animationDidStop 委托,视图消失,显然这是可怕的。

So now, the animation works, the view disappears, but sometimes, I can see the view appear and disappear faster, is like after the animation, the view appears, then the animationDidStop delegate is called, and the view disappears, obviously this is awful. What am I doing wrong?

推荐答案

那么,根据苹果示例MoveMe,这个( removedOnCompletion < b>)应该工作,但是,它似乎没有。

Well, according to the Apple sample "MoveMe", this (removedOnCompletion) should work, however, it doesn't seem to.

因此,在代码后面添加这些行:

So, add these lines after your code:


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

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

This ensures that after the animation runs, the layer is properly positioned.

这篇关于动画后,查看位置重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆