SoFunction
Updated on 2025-04-12

IOS UI learning tutorial: Use UIImageView control to create animations

This article shares with you the method of using UIImageView controls to create animations for your reference. The specific content is as follows

First add 40 tomcat pictures to the resource list: the name is cat_eat0000.jpg to cat_eat0039.jpg.
1. Define the required controls

// Define buttons, picture controls, variable array objects  UIButton *actionbuttom;
  UIImageView *imageMove;
  NSMutableArray *imgsarray;

2. Initialize each control

// image animation// Initialize UIImageView, the size is the same as the View  imageMove = [[UIImageView alloc]initWithFrame:];
// Set the initialization image of UIImageView   = [UIImage imageNamed:@"cat_eat0000.jpg"];
// Load the UIImageView to the page  [ addSubview:imageMove];
// Set the interactivity of UIImageView to yes   = YES;  
  
// Create function button// Initialize button  actionbuttom = [[UIButton alloc]initWithFrame:CGRectMake(100, 680, 218, 50)];
// Set button background color   = [UIColor yellowColor];
// Set button title  [actionbuttom setTitle:@"Start Play" forState:UIControlStateNormal];
// Set button text color  [actionbuttom setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// Add a trigger event for the button  [actionbuttom addTarget:self action:@selector(startmove:) forControlEvents:UIControlEventTouchUpInside];
// Add button to the page  [imageMove addSubview:actionbuttom];
  
  
  
// Initialize a variable array to store pictures  imgsarray = [[NSMutableArray alloc]initWithCapacity:40];
// Loop to get forty pictures from the resource and add to imgsarray.  for (int x=0; x<40; x++) {
    NSString *imgname = [NSString stringWithFormat:@"cat_eat00%.",x];
    UIImage *img = [UIImage imageNamed:imgname];
    [imgsarray addObject:img];

3. Set button to trigger animation playback

//Trigger event of button-(void)startmove:(id)sender{
// Set the animation duration   = 2;
// Set the animation image source as an image array   = imgsarray;
// Set the number of animation repetitions, 0 is infinite loop, 1 is repeated 1 time   = 1;
// Start playing  [imageMove startAnimating];
  
}

The above is all about this article. I hope it will be helpful for everyone to learn to use the UIImageView control to create animations.