UIImageview Animation Starts using for-in loop and

2019-09-03 07:50发布

问题:

I need to animate an UIimageview and when i start animation it happens after long time and i found that memory is leaked somewhere at the time of continuous animation for various image views. All I need to do is i have n number of UIviews in scroll view and each view have an custom button. 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

here is my code

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


}

With this code i can achieve what my requirement is about. But the animation starts very late because of for-in loop used

回答1:

The best way to handle this. use UIImageView's animationImages property.

  1. Create NSMutableArray object using your Image's UIImage objects.
  2. Set The Images array to the UIImageView animationImages property by
    [imageView setAnimationImages:imagesArray]

  3. [imageView startAnimating] to Start Animations.

  4. [imageView stopAnimating] to Stop Animations.
    Check the document's for more details.

For more details, check this..