【问题标题】:Changing UIImage with NSTimer? [closed]用 NSTimer 改变 UIImage? [关闭]
【发布时间】:2013-07-19 16:15:13
【问题描述】:

所以我对这个项目的最终目标是拥有几个图像和一个UIImageview,并让图像每 10 秒更改一次,我目前设置了NSTimer 和图像数组,但是我没有知道如何同时使用NSTimer 和 UIImage 视图。 到目前为止,这就是我所拥有的:

在我的 .h 中:

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController {
NSTimer *myTimer;
IBOutlet UIImageView *imageview;
}

@property (nonatomic, strong) NSArray *images;

在我的 .m 中

- (void)viewDidLoad
{
[super viewDidLoad];

myTimer = [NSTimer scheduledTimerWithTimeInterval:10.00 target:self      selector:@selector(changeImage) userInfo:nil repeats:YES];
}

- (void)changeImage
{
_images = @[@"Background 3.png", @"Background.png", @"Background 2.png", @"Background 4.png", @"Background 5.png"];

//stuck here, how do I change the image using the NStimer interval????

}

既然您已经看到了代码,我很难弄清楚 changeImage 方法中应该包含什么内容。回顾一下我想要数组中的 5 个图像在 UIImage 视图中每 10 秒循环一次。我已经尝试了很多选项,但我无法解决。任何帮助将不胜感激。

【问题讨论】:

  • 为什么这个拇指被按下了?最近有人连续投票反对这个网站。
  • @John 好吧,不是我,但我想,这个人今天早上早些时候问的一个问题是骗人的。而且这家伙甚至没有表现出对编程概念的最低理解——他不应该在编写 Objective-C。
  • @Hot Fair 关于骗子的观点,但我认为说某人不应该编写代码,因为他们不了解编程概念有点苛刻——这不是重点吗?地点?要学习如何编程?
  • 我不会说他不应该写objective-c。没人应该这么说

标签: ios objective-c uiimage


【解决方案1】:

您可以使用以下代码简单地做到这一点。

- (void)viewDidLoad
{
   [super viewDidLoad];
   _images = @[@"Background 3.png", @"Background.png", @"Background 2.png", @"Background 4.png", @"Background 5.png"];
   myTimer = [NSTimer scheduledTimerWithTimeInterval:10.00 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}

- (void)changeImage
{
  static int counter = 0;
  if([_images count] == counter+1)
  {
      counter = 0;
  }
  imageview.image = [UIImage imageNamed:[_images objectAtIndex:counter];

  counter++;
}

【讨论】:

  • 如果计数器等于 _images.count,请记住将其重置为零 - 或者使用 (counter % 5) 访问数组。
  • @DavidDoyle:当你发表评论时,我正在编辑 :) 无论如何感谢您的评论。
  • @Midhun MP 这很好,它开始工作,但是它只切换一次然后停止,我怎样才能让它继续?
  • @user2600115:你在使用上面的代码吗?如果是,那么它应该可以工作。因为我在 if 条件下重置了计数器
  • @Midhun MP ye,我已经实现了确切的代码,但它没有循环,它只是一次,是否可以让它淡入?
【解决方案2】:

把这个放到changeImage中

imageview.image = [_images objectAtIndex:self.currentIndex];
if (self.currentIndex == (_images.count-1)) {
    self.currentIndex = 0;
} else {
    self.currentIndex++;
}

这个在.h中

@property (assign) NSInteger currentIndex;

也在 viewDidLoad er 中设置了这个:

self.currentIndex = 0;

【讨论】:

    【解决方案3】:

    有几种方法可以做到这一点,我建议用户使用 NSTimeruserInfo 属性,而不是保留静态变量。这样您就不必担心跟踪单独的计数变量,您只需启动它,它就会运行(尽管出于其他原因,您可能仍想跟踪它):

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        _images = @[@"Background 3.png", @"Background.png", @"Background 2.png", @"Background 4.png", @"Background 5.png"];
    
        myTimer = [NSTimer scheduledTimerWithTimeInterval:10.00 target:self selector:@selector(changeImage:) userInfo:[NSNumber numberWithInt:0] repeats:YES];
    }
    

    然后你的回调:

    - (void)changeImage: (NSTimer *)timer
    {
        int index = timer.userInfo.intValue;
    
        imageview.image = [UIImage imageNamed:[_images objectAtIndex:index];
    
        timer.userInfo = [NSNumber numberWithInt: (index + 1) % _images.count];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多