UIImageview动画开始使用for-in循环和动画延迟

时间:2013-02-22 10:51:00

标签: ios objective-c xcode uiimageview uianimation

我需要为UIimageview设置动画,当我开始动画时,它会在很长一段时间后发生,并且我发现在连续动画时某些图像视图的内存泄漏。我需要做的就是在滚动视图中有多个UI视图,每个视图都有一个自定义按钮。当选择该按钮时,会发生动画,如果用户点击按钮,动画将继续,并且不需要再次启动,则动画正在进行中

这是我的代码

 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循环

,动画开始很晚

1 个答案:

答案 0 :(得分:5)

处理此问题的最佳方法。使用UIImageView的{​​{1}}属性。

  1. 使用图片的animationImages对象创建NSMutableArray对象。
  2. 将图像数组设置为UIImage UIImageView属性 animationImages

  3. [imageView setAnimationImages:imagesArray]启动动画。

  4. [imageView startAnimating]停止动画。
    查看文档以获取更多详细信息。
  5. 有关详细信息,请check this.