0

我需要为 UIimageview 设置动画,当我开始动画时,它会在很长一段时间后发生,我发现在各种图像视图的连续动画时内存在某处泄漏。我需要做的就是在滚动视图中有 n 个 UIview,每个视图都有一个自定义按钮。when that button is selected an animation happens and if animation is in process if the user taps on button that animation will continue and it dont need to start again

这是我的代码

 for (int i=0; i<AlphabetArray.count; i++) {

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
    view.backgroundColor=[UIColor grayColor];
    view.tag=i+1;
    [self.scrollView addSubview:view];
     UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 400, 400)];


    [image setImage:[[animationArray objectAtIndex:i]objectAtIndex:0]];
    image.tag=i+1;
    [view addSubview:image];

    UIButton *animatebutton=[[UIButton alloc]initWithFrame:CGRectMake(200, 50, 336, 310)];
    animatebutton.tag=i+1;
    [animatebutton addTarget:self action:@selector(makeAnimation:) forControlEvents:UIControlEventTouchUpInside];
    //[animatebutton setImage:[UIImage imageNamed:@"NewtDance_mov_0.png"] forState:UIControlStateNormal];
    [view addSubview:animatebutton];
}

-(void)makeAnimation:(UIImageView *)sender {

UIView *tagView=(UIView *)[self.view viewWithTag:sender.tag];



for (UIImageView * imageview in [tagView subviews]) {

    NSLog(@"Yes %d",imageview.tag);


    if ([imageview isKindOfClass:[UIImageView class]]) {

        if ([imageview isAnimating]) {
            NSLog(@"Animation Happens");

        }

        else{
            imageview.animationDuration=3.0;

            imageview.animationImages=[animationArray objectAtIndex:sender.tag-1];
            imageview.animationRepeatCount=2;
            imageview.tag=sender.tag;
            [imageview startAnimating];
        }
    }

}



NSLog(@"OK");


}

使用此代码,我可以实现我的要求。但是由于使用了 for-in 循环,动画开始得很晚

4

1 回答 1

5

处理这个问题的最好方法。使用UIImageViewanimationImages属性。

  1. NSMutableArray使用您的图像对象创建UIImage对象。
  2. 将图像数组设置为UIImageView animationImages属性
    [imageView setAnimationImages:imagesArray]

  3. [imageView startAnimating]开始动画。

  4. [imageView stopAnimating]停止动画。
    检查文档以获取更多详细信息。

有关更多详细信息,请检查此。.

于 2013-02-22T11:02:25.490 回答