uiimageView动画的问题

发布于 2024-11-25 13:24:20 字数 710 浏览 0 评论 0原文

这是我的代码:

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-12-02 13:24:20

检查 imageView == nil 是否。

if (imageView == nil) {
 imageView = [[UIImageView alloc] initWithImage:image];
}
//rest of code

Check to see if imageView == nil or not.

if (imageView == nil) {
 imageView = [[UIImageView alloc] initWithImage:image];
}
//rest of code
魔法唧唧 2024-12-02 13:24:20

每次调用 CreateNewImage 时都会创建一个新的 UIImageView

,请检查它是否已创建。

-(void) createNewImage {
if(imageView == nil) {
     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;

}

You are creating a new UIImageView every time you call CreateNewImage

instead, check to see if it has already been created.

-(void) createNewImage {
if(imageView == nil) {
     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;

}
帅的被狗咬 2024-12-02 13:24:20

如果我正确理解你的情况,会发生这样的情况:

  1. 你在createNewImage中创建一个新图像;

  2. 该图像通过以下方法进行动画处理:

    -(void)动画{
       imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
    }
    

    它为你的imageView ivar设置动画;

  3. 当您创建新图像时,2秒后,您将其重新分配给imageView ivar,以便Animation方法可以在调用时为其设置动画;< /p>

  4. 同时,您已经丢失了对之前进行动画处理的上一张图像的引用,因此动画将不再对其进行动画处理。

现在我建议尝试以这种方式更改动画:

    -(void)Animation{
       for (UIView* imgView in self.view.subviews) {
            imgView.center = CGPointMake(imgView.center.x + X, imgView.center.y + Y);
       }
    }

因此,这将简单地为主视图的任何子视图设置动画。如果您唯一的子视图是您想要设置动画的图像,那么应该没问题。

编辑:

根据您在评论中的描述,在我看来,发生的情况是 addSubview 具有立即停止所有动画的效果。我不知道这是否与从磁盘加载图像有关,但我记得不久前我也遇到过类似的问题。我看到的唯一方法是预先创建并预先添加您需要的所有 UIImageView(或允许的最大数量的图像),将它们全部隐藏,然后将它们一一再次可见。这会使程序的逻辑更加复杂,但它会起作用。

另一种方法是使用 CALayer 代替 UIVIew。最好的事情是使用 CABasicAnimation 来为图像添加动画效果。

如果你想使用 UIImageView,你可以获取 [imageView 层] 并添加这个而不是视图,如下所示:

[[self.view layer] addSublayer:[imageView layer]];

但我不确定这种方法是否有效。

希望他的帮助。

If I understand correctly your case, what happens is this:

  1. you create a new image in createNewImage;

  2. this image is animated through the method:

    -(void)Animation{
       imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
    }
    

    which animates your imageView ivar;

  3. when you create a new image, after 2 seconds, you reassign it to the imageView ivar, so that the Animation method can animate it when it is called;

  4. 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:

    -(void)Animation{
       for (UIView* imgView in self.view.subviews) {
            imgView.center = CGPointMake(imgView.center.x + X, imgView.center.y + Y);
       }
    }

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 CALayers instead of UIVIews. 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:

[[self.view layer] addSublayer:[imageView layer]];

but I am not sure that this approach would work.

Hope that his helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文