0

我想实现一个自动更改图片库。这是我在控制器中的代码。我有一个名为 image 的 uiimageview。我想将图像数组链接到我的图像视图,让它在几秒钟后自动更改。我该怎么办??

- (void)viewDidLoad{
     [super viewDidLoad];
     [self performSelector:@selector(changeImage) withObject:nil afterDelay:2];
}

-(void)changeImage {
     NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"animated-fish-1.jpg"], [UIImage imageNamed:@"tumblr_7"], [UIImage imageNamed:@"th_nature_4.jpg"],nil];
     image.animationImages = images; 
     // how to let the array of image load and link to the perform selector??
     // what should i continue from here?
 }
4

4 回答 4

4
  • 将图像存储在 ivar 中

    @interface YourClass : SomeSuperClass
    
    @property (nonatomic, strong) NSArray *images;
    
    @end
    
  • 不要启动计时器,viewDidLoad因为视图不会出现在屏幕上,所以没有意义 - 将它移到viewDidAppear:

    - (void)viewDidLoad
    {
      [super viewDidLoad];
      self.images = @[ ... ]; // or pass these into the class
      self.imageView.image = self.images[0];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
      [super viewDidAppear:animated];
    
      [NSTimer scheduledTimerWithTimeInterval:2
                                       target:self
                                     selector:@selector(changeImage:)
                                     userInfo:nil
                                      repeats:YES];
    }
    
    -(void)changeImage:(NSTimer *)timer
    {
      NSInteger currentIndex = [self.images indexOfObject:self.imageView.image];
      NSInteger nextIndex    = (currentIndex + 1) % self.images.count;
    
      self.imageView.image = self.images[nextIndex];
    
      // If for any reason you need to cancel the image rotation
      if ([self shouldCancelImageRotation]) {
        [timer invalidate];
      }
    }
    
于 2013-04-04T09:04:57.423 回答
1

要使用计时器为图像设置动画,试试这个

- (void)viewDidLoad
{
        [super viewDidLoad];
        [NSTimer scheduledTimerWithTimeInterval:1 
                                 target:self 
                               selector:@selector(animateImages) 
                               userInfo:nil 
                                repeats:YES];
        imageCount = 1;

 }

  -(void)animateImages
  {
         imageCount = imageCount + 1;
          NSString *imageName = [NSString stringWithFormat:@"image%i.png"];
          [theImage setImage:[UIImage imageNamed:imageName]];
    }
于 2013-04-04T08:56:43.310 回答
0

您应该使用 NSTimer,例如,您可以使用此方法:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/clm/NSTimer/scheduledTimerWithTimeInterval:target:selector:userInfo:重复:

此方法允许您定义要使用的选择器并每 x 秒重复一次

于 2013-04-04T08:51:27.097 回答
0

像这样尝试,取一个全局变量索引

 - (void)viewDidLoad{
         [super viewDidLoad];
          index=0;
          NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"animated-fish-1.jpg"], [UIImage imageNamed:@"tumblr_7"], [UIImage imageNamed:@"th_nature_4.jpg"],nil];
imageview.image = [images objectAtIndex:index];
          [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    }

-(void)changeImage {
      index++;
      if(index==[images count]){
        index=0;
     }
     imageview.image = [images objectAtIndex:index];
  }
于 2013-04-04T08:59:45.530 回答