uiimageView动画的问题
这是我的代码:
-(void) createNewImage {
UIImage * image = [UIImage imageNamed:@"boutonplay_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
ix=imageView.center.x;
iy=imageView.center.y;
X=(240-ix)/230;
Y=(160-iy)/230;
}
-(void)Animation{
imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
}
“CreateNewImage”每 2 秒更新一次,并且“Animation”上有一个 CADisplayLink。
我的问题是“imageView”的动画每 2 秒停止一次,因为创建了一个新的“imageView”,最后一个开始移动,2 秒后再次停止,一次又一次……我想要的是尽管创建了新的“imageView”,但仍使“imageView”继续其动画。请问我该怎么做?对不起我的英语我是法国人:/
here is my code :
-(void) createNewImage {
UIImage * image = [UIImage imageNamed:@"boutonplay_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
ix=imageView.center.x;
iy=imageView.center.y;
X=(240-ix)/230;
Y=(160-iy)/230;
}
-(void)Animation{
imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
}
"CreateNewImage" is alled every 2 seconds, and there is a CADisplayLink on "Animation".
My problem is that the animation of "imageView" stops every 2 seconds because a new "imageView" is created and this last start moving and it stops again after 2 seconds, and again and again and again ... What I would like is to make "imageView" continue his animation in spite of the creation of new "imageView".How can I do this please ? sorry for my english I'm french:/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查 imageView == nil 是否。
Check to see if imageView == nil or not.
每次调用 CreateNewImage 时都会创建一个新的 UIImageView
,请检查它是否已创建。
You are creating a new UIImageView every time you call CreateNewImage
instead, check to see if it has already been created.
如果我正确理解你的情况,会发生这样的情况:
你在
createNewImage
中创建一个新图像;该图像通过以下方法进行动画处理:
它为你的
imageView
ivar设置动画;当您创建新图像时,2秒后,您将其重新分配给
imageView
ivar,以便Animation
方法可以在调用时为其设置动画;< /p>同时,您已经丢失了对之前进行动画处理的上一张图像的引用,因此动画将不再对其进行动画处理。
现在我建议尝试以这种方式更改动画:
因此,这将简单地为主视图的任何子视图设置动画。如果您唯一的子视图是您想要设置动画的图像,那么应该没问题。
编辑:
根据您在评论中的描述,在我看来,发生的情况是
addSubview
具有立即停止所有动画的效果。我不知道这是否与从磁盘加载图像有关,但我记得不久前我也遇到过类似的问题。我看到的唯一方法是预先创建并预先添加您需要的所有 UIImageView(或允许的最大数量的图像),将它们全部隐藏,然后将它们一一再次可见。这会使程序的逻辑更加复杂,但它会起作用。另一种方法是使用 CALayer 代替 UIVIew。最好的事情是使用 CABasicAnimation 来为图像添加动画效果。
如果你想使用 UIImageView,你可以获取 [imageView 层] 并添加这个而不是视图,如下所示:
但我不确定这种方法是否有效。
希望他的帮助。
If I understand correctly your case, what happens is this:
you create a new image in
createNewImage
;this image is animated through the method:
which animates your
imageView
ivar;when you create a new image, after 2 seconds, you reassign it to the
imageView
ivar, so that theAnimation
method can animate it when it is called;at the same time, you have lost the reference to your previous image that you were animating before, so Animation will not animate it anymore.
Now what I suggest is trying to change
Animation
in this way:So, this will simply animate any subview of your main view. If your only subviews are the images you want to animate, it should be fine.
EDIT:
From your description in the comments, it seems to me that what happens is that
addSubview
has the effect of stopping shortly all animations. I don't know if this is related to loading the images from disk, but I remember I also experienced a similar problem time ago. The only approach I see is pre-creating and pre-adding all the UIImageViews you need (or a maximum number of allowed images), make them all hidden and then making them visible again one by one. This would make the logic of your program more complex, but it would work.Another approach could be using
CALayer
s instead ofUIVIew
s. Best thing would be using a CABasicAnimation to animate your images.If you want to use UIImageView, you could get
[imageView layer]
and add this one instead of the view, like this:but I am not sure that this approach would work.
Hope that his helps.